From cfd6fb939c53ccfee7806870dc92ca43f463ef55 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 | 93 +++++++++++++++++++ 2 files changed, 134 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..8f067e3 --- /dev/null +++ b/Domain/Service/Repository/RepositoryInterface.php @@ -0,0 +1,41 @@ + + * @template-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..942ebce --- /dev/null +++ b/Infrastructure/Persistence/Doctrine/Repository/DoctrineRepository.php @@ -0,0 +1,93 @@ + + */ +class DoctrineRepository extends ServiceEntityRepository implements RepositoryInterface +{ + public function __construct( + ManagerRegistry $registry, + string $entityClass, + private EntityPersisterInterface $entityPersister, + ) { + parent::__construct( + $registry, + $entityClass + ); + } + + /** + * @param DtoT $dto + * @param EntityT|null $entity + * @return EntityT + */ + public function persistDto(DataTransferObjectInterface $dto, EntityInterface $entity = null, $dispatchImmediately = false): EntityInterface + { + return + $this + ->entityPersister + ->persistDto( + $dto, + $entity, + $dispatchImmediately + ); + } + + /** + * @param EntityT $entity + */ + public function persist(EntityInterface $entity, bool $dispatchImmediately = false): void + { + $this + ->entityPersister + ->persist( + $entity, + $dispatchImmediately + ); + } + + /** + * @param EntityT $entity + */ + public function remove(EntityInterface $entity): void + { + $this + ->entityPersister + ->remove( + $entity + ); + } + + /** + * @param EntityT[] $entities + */ + public function removeFromArray(array $entities): void + { + $this + ->entityPersister + ->removeFromArray( + $entities + ); + } + + public function dispatchQueued(): void + { + $this->entityPersister->dispatchQueued(); + } +} \ No newline at end of file