Skip to content

Commit

Permalink
add persist methods in repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Sep 29, 2023
1 parent b1198e6 commit 8a7d8e6
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Domain/Service/Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Ivoz\Core\Domain\Service\Repository;

use Doctrine\Common\Collections\Selectable;
use Doctrine\Persistence\ObjectRepository;
use Ivoz\Core\Domain\DataTransferObjectInterface;
use Ivoz\Core\Domain\Model\EntityInterface;

/**
* @template EntityT of EntityInterface
* @template DtoT of DataTransferObjectInterface
* @extends ObjectRepository<EntityT>
* @extends Selectable<int, EntityT>
*/
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Ivoz\Core\Infrastructure\Persistence\Doctrine\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Ivoz\Core\Domain\DataTransferObjectInterface;
use Ivoz\Core\Domain\Model\EntityInterface;
use Ivoz\Core\Domain\Service\EntityPersisterInterface;
use Ivoz\Core\Domain\Service\Repository\RepositoryInterface;

/**
* CompanyDoctrineRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*
* @template T of EntityInterface
* @template DtoT of DataTransferObjectInterface
* @extends ServiceEntityRepository<T>
* @implements RepositoryInterface<T, DtoT>
*/
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();
}
}

0 comments on commit 8a7d8e6

Please sign in to comment.