From 25a6acab2155be7c69ed082e0de66243d47f0087 Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Thu, 28 Sep 2023 13:52:30 +0200 Subject: [PATCH] add persist methods in repositories --- .../Repository/RepositoryInterface.php | 41 ++++++++ .../Repository/DoctrineRepository.php | 94 +++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Domain/Service/Repository/RepositoryInterface.php create mode 100644 Infrastructure/Persistence/Doctrine/Repository/DoctrineRepository.php diff --git a/Domain/Service/Repository/RepositoryInterface.php b/Domain/Service/Repository/RepositoryInterface.php new file mode 100644 index 0000000..e30bf8f --- /dev/null +++ b/Domain/Service/Repository/RepositoryInterface.php @@ -0,0 +1,41 @@ + + * @extends Selectable + */ +interface RepositoryInterface extends ObjectRepository, Selectable +{ + /** + * @param DtoT $dto + * @param EntityT|null $entity + * @return EntityT + */ + public function persistDto(DataTransferObjectInterface $dto, EntityInterface $entity = null, $dispatchImmediately = false): EntityInterface; + + /** + * @param EntityT $entity + */ + public function persist(EntityInterface $entity, bool $dispatchImmediately = false): void; + + /** + * @param EntityT $entity + */ + public function remove(EntityInterface $entity): void; + + /** + * @param EntityT[] $entities + */ + public function removeFromArray(array $entities): void; + + public function dispatchQueued(): void; +} \ No newline at end of file diff --git a/Infrastructure/Persistence/Doctrine/Repository/DoctrineRepository.php b/Infrastructure/Persistence/Doctrine/Repository/DoctrineRepository.php new file mode 100644 index 0000000..3611b99 --- /dev/null +++ b/Infrastructure/Persistence/Doctrine/Repository/DoctrineRepository.php @@ -0,0 +1,94 @@ + + * @implements RepositoryInterface + */ +class DoctrineRepository extends ServiceEntityRepository implements RepositoryInterface +{ + public function __construct( + ManagerRegistry $registry, + string $entityClass, + private EntityPersisterInterface $entityPersister, + ) { + parent::__construct( + $registry, + $entityClass + ); + } + + /** + * @param DtoT $dto + * @param T|null $entity + * @return T + */ + public function persistDto(DataTransferObjectInterface $dto, EntityInterface $entity = null, $dispatchImmediately = false): EntityInterface + { + return + $this + ->entityPersister + ->persistDto( + $dto, + $entity, + $dispatchImmediately + ); + } + + /** + * @param T $entity + */ + public function persist(EntityInterface $entity, bool $dispatchImmediately = false): void + { + $this + ->entityPersister + ->persist( + $entity, + $dispatchImmediately + ); + } + + /** + * @param T $entity + */ + public function remove(EntityInterface $entity): void + { + $this + ->entityPersister + ->remove( + $entity + ); + } + + /** + * @param T[] $entities + */ + public function removeFromArray(array $entities): void + { + $this + ->entityPersister + ->removeFromArray( + $entities + ); + } + + public function dispatchQueued(): void + { + $this->entityPersister->dispatchQueued(); + } +} \ No newline at end of file