Passed
Push — master ( 79bf3c...52a24b )
by Alex
01:04 queued 13s
created

PersistServiceFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
c 1
b 0
f 0
dl 0
loc 88
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventDispatcher() 0 25 5
A __invoke() 0 32 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Repository\Persistence;
6
7
use Arp\DoctrineEntityRepository\Persistence\PersistService;
8
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface;
9
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Laminas\ServiceManager\ServiceLocatorInterface;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\ContainerInterface;
17
use Psr\Container\NotFoundExceptionInterface;
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * @author  Alex Patterson <[email protected]>
22
 * @package Arp\LaminasDoctrine\Factory\Repository\Persistence
23
 */
24
final class PersistServiceFactory extends AbstractFactory
25
{
26
    use EntityManagerFactoryProviderTrait;
27
    use FactoryLoggerProviderTrait;
28
29
    /**
30
     * @param ContainerInterface&ServiceLocatorInterface $container
31
     * @param string                                     $requestedName
32
     * @param array<mixed>|null                          $options
33
     *
34
     * @return PersistServiceInterface
35
     *
36
     * @throws ServiceNotCreatedException
37
     * @throws ServiceNotFoundException
38
     * @throws ContainerExceptionInterface
39
     * @throws NotFoundExceptionInterface
40
     */
41
    public function __invoke(
42
        ContainerInterface $container,
43
        string $requestedName,
44
        array $options = null
45
    ): PersistServiceInterface {
46
        $options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []);
47
48
        $entityName = $options['entity_name'] ?? null;
49
        if (empty($entityName)) {
50
            throw new ServiceNotCreatedException(
51
                sprintf(
52
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
53
                    $requestedName
54
                )
55
            );
56
        }
57
58
        $entityManager = $options['entity_manager'] ?? null;
59
        if (empty($entityManager)) {
60
            throw new ServiceNotCreatedException(
61
                sprintf(
62
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
63
                    $requestedName
64
                )
65
            );
66
        }
67
68
        return new PersistService(
69
            $entityName,
70
            $this->getEntityManager($container, $entityManager, $requestedName),
71
            $this->getEventDispatcher($container, $options['event_dispatcher'] ?? [], $requestedName),
72
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
73
        );
74
    }
75
76
    /**
77
     * @param ServiceLocatorInterface                      $container
78
     * @param EventDispatcherInterface|string|array<mixed> $eventDispatcher
79
     * @param string                                       $serviceName
80
     *
81
     * @return EventDispatcherInterface
82
     *
83
     * @throws ServiceNotCreatedException
84
     * @throws ServiceNotFoundException
85
     * @throws ContainerExceptionInterface
86
     */
87
    private function getEventDispatcher(
88
        ServiceLocatorInterface $container,
89
        $eventDispatcher,
90
        string $serviceName
91
    ): EventDispatcherInterface {
92
        if (is_string($eventDispatcher)) {
93
            $eventDispatcher = $this->getService($container, $eventDispatcher, $serviceName);
94
        }
95
96
        if (is_array($eventDispatcher)) {
97
            $eventDispatcher = $this->buildService($container, 'EntityEventDispatcher', $eventDispatcher, $serviceName);
98
        }
99
100
        if (!$eventDispatcher instanceof EventDispatcherInterface) {
101
            throw new ServiceNotCreatedException(
102
                sprintf(
103
                    'The event dispatcher must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
104
                    EventDispatcherInterface::class,
105
                    is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher),
106
                    $serviceName
107
                )
108
            );
109
        }
110
111
        return $eventDispatcher;
112
    }
113
}
114