PersistServiceFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 31 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Repository\Persistence;
6
7
use Arp\Entity\EntityInterface;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
9
use Arp\LaminasDoctrine\Repository\Persistence\PersistService;
10
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceInterface;
11
use Arp\LaminasFactory\AbstractFactory;
12
use Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait;
13
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
14
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
15
use Laminas\ServiceManager\ServiceLocatorInterface;
16
use Psr\Container\ContainerExceptionInterface;
17
use Psr\Container\ContainerInterface;
18
use Psr\Container\NotFoundExceptionInterface;
19
20
final class PersistServiceFactory extends AbstractFactory
21
{
22
    use EntityManagerFactoryProviderTrait;
23
    use FactoryLoggerProviderTrait;
24
25
    /**
26
     * @param ContainerInterface&ServiceLocatorInterface $container
27
     * @param array<mixed>|null $options
28
     *
29
     * @return PersistServiceInterface<EntityInterface>
30
     *
31
     * @throws ServiceNotCreatedException
32
     * @throws ServiceNotFoundException
33
     * @throws ContainerExceptionInterface
34
     * @throws NotFoundExceptionInterface
35
     */
36
    public function __invoke(
37
        ContainerInterface $container,
38
        string $requestedName,
39
        array $options = null
40
    ): PersistServiceInterface {
41
        $options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []);
42
43
        $entityName = $options['entity_name'] ?? null;
44
        if (empty($entityName)) {
45
            throw new ServiceNotCreatedException(
46
                sprintf(
47
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
48
                    $requestedName
49
                )
50
            );
51
        }
52
53
        $entityManager = $options['entity_manager'] ?? null;
54
        if (empty($entityManager)) {
55
            throw new ServiceNotCreatedException(
56
                sprintf(
57
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
58
                    $requestedName
59
                )
60
            );
61
        }
62
63
        return new PersistService(
64
            $entityName,
65
            $this->getEntityManager($container, $entityManager, $requestedName),
66
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
67
        );
68
    }
69
}
70