-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…asses added.
- Loading branch information
automatix
committed
Jul 19, 2018
1 parent
7db8179
commit a0c3884
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
namespace App\Base\Repository; | ||
|
||
use App\Base\Entity\AbstractEntity; | ||
use App\Base\Exceptions\GeneralErrorContextCode; | ||
use App\Base\Exceptions\GeneralException; | ||
use App\Base\Selectors\AbstractSelector; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; | ||
use Doctrine\Common\Persistence\ObjectRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
/** | ||
* The class AbstractRepository is the parent class of all Repository classes | ||
* and proveds the basic functionality for the data access. | ||
* | ||
* @package App\Base\Repository | ||
* @author Ilya Khanataev <[email protected]> | ||
*/ | ||
abstract class AbstractRepository | ||
{ | ||
|
||
/** @var EntityManagerInterface */ | ||
private $entityManager; | ||
|
||
/** | ||
* AbstractRepository constructor. | ||
* @param EntityManagerInterface $entityManager | ||
*/ | ||
public function __construct(EntityManagerInterface $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* @param AbstractSelector $id | ||
* @return AbstractEntity | ||
* @throws GeneralException | ||
*/ | ||
public function find(AbstractSelector $id) | ||
{ | ||
$this->checkSelector($id); | ||
return $this->getRepository()->find($id); | ||
} | ||
|
||
/** | ||
* @return AbstractEntity[] | ||
*/ | ||
public function findAll() | ||
{ | ||
return $this->getRepository()->findAll(); | ||
} | ||
|
||
/** | ||
* @return string The FQCN of the Entity class, the repository is responsible for. | ||
*/ | ||
abstract public function getEntityClass(); | ||
|
||
/** | ||
* @return string The FQCN of the Selector for the managed Entity. | ||
*/ | ||
abstract public function getEntitySelectorClass(); | ||
|
||
/** | ||
* @return EntityManagerInterface | ||
*/ | ||
protected function getEntityManager(): EntityManagerInterface | ||
{ | ||
return $this->entityManager; | ||
} | ||
|
||
/** | ||
* @param EntityManagerInterface $entityManager | ||
* @return AbstractRepository | ||
*/ | ||
protected function setEntityManager(EntityManagerInterface $entityManager): AbstractRepository | ||
{ | ||
$this->entityManager = $entityManager; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return ServiceEntityRepositoryInterface | ||
*/ | ||
protected function getRepository(): ObjectRepository | ||
{ | ||
return $this->entityManager->getRepository($this->getEntityClass()); | ||
} | ||
|
||
/** | ||
* @param AbstractSelector $id | ||
* @throws GeneralException | ||
*/ | ||
private function checkSelector(AbstractSelector $id): void | ||
{ | ||
$entitySelectorClass = $this->getEntitySelectorClass(); | ||
if (!($id instanceof $entitySelectorClass)) { | ||
throw new GeneralException(null, GeneralErrorContextCode::INVALID_ARGUMENT()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace App\Base\Repository; | ||
|
||
use App\Base\Entity\Chat; | ||
use App\Base\Selectors\ChatSelector; | ||
|
||
class ChatRepository extends AbstractRepository | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getEntityClass() | ||
{ | ||
return Chat::class; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getEntitySelectorClass() | ||
{ | ||
return ChatSelector::class; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace App\Base\Repository; | ||
|
||
use App\Base\Entity\User; | ||
use App\Base\Selectors\UserSelector; | ||
|
||
class UserRepository extends AbstractRepository | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getEntityClass() | ||
{ | ||
return User::class; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getEntitySelectorClass() | ||
{ | ||
return UserSelector::class; | ||
} | ||
|
||
} |
Empty file.