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 @@ +