Passed
Pull Request — master (#4)
by Alex
08:39
created

PersistServiceFactory::getEventDispatcher()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
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\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
8
use Arp\LaminasDoctrine\Repository\Persistence\PersistService;
9
use Arp\LaminasDoctrine\Repository\Persistence\PersistServiceInterface;
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
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
    use FactoryLoggerProviderTrait;
27
28
    /**
29
     * @param ContainerInterface&ServiceLocatorInterface $container
30
     * @param string                                     $requestedName
31
     * @param array<mixed>|null                          $options
32
     *
33
     * @return PersistServiceInterface
34
     *
35
     * @throws ServiceNotCreatedException
36
     * @throws ServiceNotFoundException
37
     * @throws ContainerExceptionInterface
38
     * @throws NotFoundExceptionInterface
39
     */
40
    public function __invoke(
41
        ContainerInterface $container,
42
        string $requestedName,
43
        array $options = null
44
    ): PersistServiceInterface {
45
        $options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []);
46
47
        $entityName = $options['entity_name'] ?? null;
48
        if (empty($entityName)) {
49
            throw new ServiceNotCreatedException(
50
                sprintf(
51
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
52
                    $requestedName
53
                )
54
            );
55
        }
56
57
        $entityManager = $options['entity_manager'] ?? null;
58
        if (empty($entityManager)) {
59
            throw new ServiceNotCreatedException(
60
                sprintf(
61
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
62
                    $requestedName
63
                )
64
            );
65
        }
66
67
        return new PersistService(
68
            $entityName,
69
            $this->getEntityManager($container, $entityManager, $requestedName),
70
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
71
        );
72
    }
73
}
74