Passed
Pull Request — master (#2)
by Alex
08:14
created

PersistServiceFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 33
dl 0
loc 85
rs 10
c 0
b 0
f 0

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\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\ContainerInterface;
16
use Psr\EventDispatcher\EventDispatcherInterface;
17
18
/**
19
 * @deprecated
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;
0 ignored issues
show
Deprecated Code introduced by
The trait Arp\LaminasDoctrine\Fact...gerFactoryProviderTrait has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

26
    use /** @scrutinizer ignore-deprecated */ EntityManagerFactoryProviderTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
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
     */
39
    public function __invoke(
40
        ContainerInterface $container,
41
        string $requestedName,
42
        array $options = null
43
    ): PersistServiceInterface {
44
        $options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []);
45
46
        $entityName = $options['entity_name'] ?? null;
47
        if (empty($entityName)) {
48
            throw new ServiceNotCreatedException(
49
                sprintf(
50
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
51
                    $requestedName
52
                )
53
            );
54
        }
55
56
        $entityManager = $options['entity_manager'] ?? null;
57
        if (empty($entityManager)) {
58
            throw new ServiceNotCreatedException(
59
                sprintf(
60
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
61
                    $requestedName
62
                )
63
            );
64
        }
65
66
        return new PersistService(
67
            $entityName,
68
            $this->getEntityManager($container, $entityManager, $requestedName),
69
            $this->getEventDispatcher($container, $options['event_dispatcher'] ?? [], $requestedName),
70
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
71
        );
72
    }
73
74
    /**
75
     * @param ServiceLocatorInterface                      $container
76
     * @param EventDispatcherInterface|string|array<mixed> $eventDispatcher
77
     * @param string                                       $serviceName
78
     *
79
     * @return EventDispatcherInterface
80
     *
81
     * @throws ServiceNotCreatedException
82
     * @throws ServiceNotFoundException
83
     */
84
    private function getEventDispatcher(
85
        ServiceLocatorInterface $container,
86
        $eventDispatcher,
87
        string $serviceName
88
    ): EventDispatcherInterface {
89
        if (is_string($eventDispatcher)) {
90
            $eventDispatcher = $this->getService($container, $eventDispatcher, $serviceName);
91
        }
92
93
        if (is_array($eventDispatcher)) {
94
            $eventDispatcher = $this->buildService($container, 'EntityEventDispatcher', $eventDispatcher, $serviceName);
95
        }
96
97
        if (!$eventDispatcher instanceof EventDispatcherInterface) {
98
            throw new ServiceNotCreatedException(
99
                sprintf(
100
                    'The event dispatcher must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
101
                    EventDispatcherInterface::class,
102
                    is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher),
103
                    $serviceName
104
                )
105
            );
106
        }
107
108
        return $eventDispatcher;
109
    }
110
}
111