Test Failed
Pull Request — master (#2)
by Alex
02:44
created

PersistServiceFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 32
rs 9.6666
cc 3
nc 3
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 Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Laminas\ServiceManager\ServiceLocatorInterface;
14
use Psr\EventDispatcher\EventDispatcherInterface;
15
use Psr\Log\NullLogger;
16
17
/**
18
 * @author  Alex Patterson <[email protected]>
19
 * @package Arp\LaminasDoctrine\Factory\Repository\Persistence
20
 */
21
final class PersistServiceFactory extends AbstractFactory
22
{
23
    use EntityManagerFactoryProviderTrait;
24
25
    /**
26
     * @param ServiceLocatorInterface $container
27
     * @param string                  $requestedName
28
     * @param array<mixed>|null       $options
29
     *
30
     * @return PersistServiceInterface
31
     *
32
     * @throws ServiceNotCreatedException
33
     * @throws ServiceNotFoundException
34
     */
35
    public function __invoke(
36
        ServiceLocatorInterface $container,
37
        string $requestedName,
38
        array $options = null
39
    ): PersistServiceInterface {
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 ServiceLocatorInterface                      $container
72
     * @param EventDispatcherInterface|string|array<mixed> $eventDispatcher
73
     * @param string                                       $serviceName
74
     *
75
     * @return EventDispatcherInterface
76
     *
77
     * @throws ServiceNotCreatedException
78
     * @throws ServiceNotFoundException
79
     */
80
    private function getEventDispatcher(
81
        ServiceLocatorInterface $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