Skip to content

Commit

Permalink
Refactor existing PersistServiceInterface and related PersistService …
Browse files Browse the repository at this point in the history
…to use a generic interface; which is independent of a specific entity name
  • Loading branch information
alex-patterson-webdev committed Jun 26, 2023
1 parent 05c7ef7 commit 4f42b16
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 130 deletions.
3 changes: 3 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Arp\LaminasDoctrine\Factory\Validator\IsEntityNoMatchValidatorFactory;
use Arp\LaminasDoctrine\Hydrator\EntityHydrator;
use Arp\LaminasDoctrine\Repository\Persistence\PersistService;
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceInterface;
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceManager;
use Arp\LaminasDoctrine\Repository\Query\QueryService;
use Arp\LaminasDoctrine\Repository\Query\QueryServiceManager;
Expand Down Expand Up @@ -118,6 +119,8 @@
// Connection
ConnectionManager::class => ConnectionManagerInterface::class,
ConnectionFactory::class => ConnectionFactoryInterface::class,

PersistServiceInterface::class => PersistService::class,
],
'factories' => [
// Config
Expand Down
7 changes: 3 additions & 4 deletions src/Factory/Repository/EntityRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Arp\Entity\EntityInterface;
use Arp\LaminasDoctrine\Repository\EntityRepository;
use Arp\LaminasDoctrine\Repository\EntityRepositoryInterface;
use Arp\LaminasDoctrine\Repository\Persistence\PersistService;
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceInterface;
use Arp\LaminasDoctrine\Repository\Query\QueryService;
use Arp\LaminasDoctrine\Repository\Query\QueryServiceInterface;
Expand Down Expand Up @@ -39,7 +38,7 @@ final class EntityRepositoryFactory extends AbstractFactory
'logger' => null,
],
'persist_service' => [
'service_name' => PersistService::class,
'service_name' => PersistServiceInterface::class,
'logger' => null,
],
];
Expand Down Expand Up @@ -142,14 +141,14 @@ private function getPersistService(
string $serviceName
): PersistServiceInterface {
$options = array_replace_recursive(
$this->getServiceOptions($container, PersistService::class),
$this->getServiceOptions($container, PersistServiceInterface::class),
$options
);
$options['entity_name'] ??= $entityName;

return $this->buildService(
$container,
$options['service_name'] ?? PersistService::class,
$options['service_name'] ?? PersistServiceInterface::class,
$options,
$serviceName
);
Expand Down
11 changes: 0 additions & 11 deletions src/Factory/Repository/Persistence/PersistServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public function __invoke(
): PersistServiceInterface {
$options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []);

$entityName = $options['entity_name'] ?? null;
if (empty($entityName)) {
throw new ServiceNotCreatedException(
sprintf(
'The required \'entity_name\' configuration option is missing for service \'%s\'',
$requestedName
)
);
}

$entityManager = $options['entity_manager'] ?? null;
if (empty($entityManager)) {
throw new ServiceNotCreatedException(
Expand All @@ -61,7 +51,6 @@ public function __invoke(
}

return new PersistService(
$entityName,
$this->getEntityManager($container, $entityManager, $requestedName),
$this->getLogger($container, $options['logger'] ?? null, $requestedName)
);
Expand Down
19 changes: 19 additions & 0 deletions src/Repository/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @param Entity $entity
* @param array<mixed> $options
*
Expand All @@ -163,6 +165,8 @@ public function save(EntityInterface $entity, array $options = []): EntityInterf
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @param iterable<Entity> $collection
* @param array<mixed> $options
*
Expand All @@ -184,6 +188,8 @@ public function saveCollection(iterable $collection, array $options = []): itera
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @param Entity $entity
* @param array<mixed> $options
*
Expand All @@ -206,6 +212,8 @@ public function delete(EntityInterface $entity, array $options = []): bool
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @param iterable<int, Entity> $collection
* @param array<mixed> $options
*
Expand All @@ -225,6 +233,8 @@ public function deleteCollection(iterable $collection, array $options = []): int
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @throws EntityRepositoryException
*/
public function clear(): void
Expand All @@ -241,6 +251,8 @@ public function clear(): void
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @param Entity $entity
*
* @throws EntityRepositoryException
Expand All @@ -262,6 +274,8 @@ public function refresh(EntityInterface $entity): void
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @throws EntityRepositoryException
*/
public function beginTransaction(): void
Expand All @@ -278,6 +292,8 @@ public function beginTransaction(): void
}

/**
* @deprecated Please use the PersistServiceInterface directly
*
* @throws EntityRepositoryException
*/
public function commitTransaction(): void
Expand All @@ -293,6 +309,9 @@ public function commitTransaction(): void
}
}

/**
* @deprecated Please use the PersistServiceInterface directly
*/
public function rollbackTransaction(): void
{
$this->persistService->rollbackTransaction();
Expand Down
Loading

0 comments on commit 4f42b16

Please sign in to comment.