Test Failed
Pull Request — master (#2)
by Alex
02:29
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\LaminasDoctrine\Factory\Service\EntityManagerFactoryProviderTrait;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Interop\Container\ContainerInterface;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Laminas\ServiceManager\ServiceManager;
15
use Psr\EventDispatcher\EventDispatcherInterface;
16
use Psr\Log\NullLogger;
17
18
/**
19
 * @author  Alex Patterson <[email protected]>
20
 * @package Arp\LaminasDoctrine\Factory\Repository\Persistence
21
 */
22
final class PersistServiceFactory extends AbstractFactory
23
{
24
    use EntityManagerFactoryProviderTrait;
25
26
    /**
27
     * @noinspection PhpMissingParamTypeInspection
28
     *
29
     * @param ContainerInterface $container
30
     * @param string             $requestedName
31
     * @param array|null         $options
32
     *
33
     * @return PersistServiceInterface
34
     *
35
     * @throws ServiceNotCreatedException
36
     * @throws ServiceNotFoundException
37
     */
38
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
39
    {
40
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
41
42
        $entityName = $options['entity_name'] ?? null;
43
        if (empty($entityName)) {
44
            throw new ServiceNotCreatedException(
45
                sprintf(
46
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
47
                    $requestedName
48
                )
49
            );
50
        }
51
52
        $entityManager = $options['entity_manager'] ?? null;
53
        if (empty($entityManager)) {
54
            throw new ServiceNotCreatedException(
55
                sprintf(
56
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
57
                    $requestedName
58
                )
59
            );
60
        }
61
62
        return new PersistService(
63
            $entityName,
64
            $this->getEntityManager($container, $entityManager, $requestedName),
65
            $this->getEventDispatcher($container, $options['event_dispatcher'] ?? [], $requestedName),
66
            new NullLogger()
67
        );
68
    }
69
70
    /**
71
     * @param ContainerInterface|ServiceManager     $container
72
     * @param EventDispatcherInterface|string|array $eventDispatcher
73
     * @param string                                $serviceName
74
     *
75
     * @return EventDispatcherInterface
76
     *
77
     * @throws ServiceNotCreatedException
78
     * @throws ServiceNotFoundException
79
     */
80
    private function getEventDispatcher(
81
        ContainerInterface $container,
82
        $eventDispatcher,
83
        string $serviceName
84
    ): EventDispatcherInterface {
85
        if (is_string($eventDispatcher)) {
86
            $eventDispatcher = $this->getService($container, $eventDispatcher, $serviceName);
87
        }
88
89
        if (is_array($eventDispatcher)) {
90
            $eventDispatcher = $this->buildService($container, 'EntityEventDispatcher', $eventDispatcher, $serviceName);
91
        }
92
93
        if (!$eventDispatcher instanceof EventDispatcherInterface) {
94
            throw new ServiceNotCreatedException(
95
                sprintf(
96
                    'The event dispatcher must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
97
                    EventDispatcherInterface::class,
98
                    is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher),
99
                    $serviceName
100
                )
101
            );
102
        }
103
104
        return $eventDispatcher;
105
    }
106
}
107