Passed
Pull Request — master (#2)
by Alex
02:56
created

PersistServiceFactory::getEventDispatcher()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 25
rs 9.5555
cc 5
nc 8
nop 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\EventDispatcher\EventDispatcher;
10
use Arp\LaminasDoctrine\Factory\Service\EntityManagerFactoryProviderTrait;
11
use Arp\LaminasFactory\AbstractFactory;
12
use Interop\Container\ContainerInterface;
13
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
14
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
15
use Laminas\ServiceManager\ServiceManager;
16
use Psr\EventDispatcher\EventDispatcherInterface;
17
use Psr\Log\NullLogger;
18
19
/**
20
 * @author  Alex Patterson <[email protected]>
21
 * @package Arp\LaminasDoctrine\Factory\Repository\Persistence
22
 */
23
final class PersistServiceFactory extends AbstractFactory
24
{
25
    use EntityManagerFactoryProviderTrait;
26
27
    /**
28
     * @noinspection PhpMissingParamTypeInspection
29
     *
30
     * @param ContainerInterface $container
31
     * @param string             $requestedName
32
     * @param array|null         $options
33
     *
34
     * @return PersistServiceInterface
35
     *
36
     * @throws ServiceNotCreatedException
37
     * @throws ServiceNotFoundException
38
     */
39
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40
    {
41
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
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->getEventDispatcher($container, $options['event_dispatcher'] ?? [], $requestedName),
67
            new NullLogger()
68
        );
69
    }
70
71
    /**
72
     * @param ContainerInterface|ServiceManager     $container
73
     * @param EventDispatcherInterface|string|array $eventDispatcher
74
     * @param string                                $serviceName
75
     *
76
     * @return EventDispatcherInterface
77
     *
78
     * @throws ServiceNotCreatedException
79
     * @throws ServiceNotFoundException
80
     */
81
    private function getEventDispatcher(
82
        ContainerInterface $container,
83
        $eventDispatcher,
84
        string $serviceName
85
    ): EventDispatcherInterface {
86
        if (is_string($eventDispatcher)) {
87
            $eventDispatcher = $this->getService($container, $eventDispatcher, $serviceName);
88
        }
89
90
        if (is_array($eventDispatcher)) {
91
            $eventDispatcher = $this->buildService($container, 'EntityEventDispatcher', $eventDispatcher, $serviceName);
92
        }
93
94
        if (!$eventDispatcher instanceof EventDispatcherInterface) {
95
            throw new ServiceNotCreatedException(
96
                sprintf(
97
                    'The event dispatcher must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
98
                    EventDispatcherInterface::class,
99
                    is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher),
100
                    $serviceName
101
                )
102
            );
103
        }
104
105
        return $eventDispatcher;
106
    }
107
}
108