From a0c3884740a69cd402c2fd8f0d6e91c16d200f68 Mon Sep 17 00:00:00 2001 From: automatix Date: Thu, 19 Jul 2018 21:34:58 +0200 Subject: [PATCH] #5 The root `Repository` class implemented Some first `Repository` classes added. --- src/Base/Repository/AbstractRepository.php | 101 +++++++++++++++++++++ src/Base/Repository/ChatRepository.php | 26 ++++++ src/Base/Repository/UserRepository.php | 26 ++++++ src/Base/Repository/empty | 0 4 files changed, 153 insertions(+) create mode 100644 src/Base/Repository/AbstractRepository.php create mode 100644 src/Base/Repository/ChatRepository.php create mode 100644 src/Base/Repository/UserRepository.php delete mode 100644 src/Base/Repository/empty diff --git a/src/Base/Repository/AbstractRepository.php b/src/Base/Repository/AbstractRepository.php new file mode 100644 index 0000000..ff33e69 --- /dev/null +++ b/src/Base/Repository/AbstractRepository.php @@ -0,0 +1,101 @@ + + */ +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()); + } + } + +} \ No newline at end of file diff --git a/src/Base/Repository/ChatRepository.php b/src/Base/Repository/ChatRepository.php new file mode 100644 index 0000000..ccd6a5a --- /dev/null +++ b/src/Base/Repository/ChatRepository.php @@ -0,0 +1,26 @@ +