OrmExecutorFactory::getPurger()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 25
rs 9.5555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\DataFixture;
6
7
use Arp\LaminasDoctrine\Data\Repository\ReferenceRepository;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
9
use Arp\LaminasDoctrine\Factory\Service\EntityManager\ObjectManagerArgvInputProviderTrait;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
12
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
13
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
14
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
15
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
16
use Psr\Container\ContainerExceptionInterface;
17
use Psr\Container\ContainerInterface;
18
use Psr\Container\NotFoundExceptionInterface;
19
20
final class OrmExecutorFactory extends AbstractFactory
21
{
22
    use ObjectManagerArgvInputProviderTrait;
23
    use EntityManagerFactoryProviderTrait;
24
25
    /**
26
     * @param ContainerInterface $container
27
     * @param string $requestedName
28
     * @param array<string, mixed>|null $options
29
     *
30
     * @return ORMExecutor
31
     *
32
     * @throws ServiceNotCreatedException
33
     * @throws ServiceNotFoundException
34
     * @throws ContainerExceptionInterface
35
     * @throws NotFoundExceptionInterface
36
     */
37
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): ORMExecutor
38
    {
39
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
40
41
        $entityManagerName = $options['entity_manager'] ?? null;
42
        if (empty($entityManagerName)) {
43
            $entityManagerName = $this->getEntityManagerArgvInput();
44
        }
45
46
        if (empty($entityManagerName)) {
47
            throw new ServiceNotCreatedException(
48
                sprintf(
49
                    'The \'entity_manager\' configuration option could not be found for service \'%s\'',
50
                    $requestedName
51
                )
52
            );
53
        }
54
55
        $entityManager = $this->getEntityManager($container, $entityManagerName, $requestedName);
56
57
        $executor = new ORMExecutor(
58
            $entityManager,
59
            $this->getPurger($container, $options['purger'] ?? null, $requestedName),
60
        );
61
62
        $executor->setReferenceRepository(new ReferenceRepository($entityManager));
63
64
        return $executor;
65
    }
66
67
    /**
68
     * @throws ServiceNotCreatedException
69
     * @throws ServiceNotFoundException
70
     * @throws ContainerExceptionInterface
71
     */
72
    private function getPurger(
73
        ContainerInterface $container,
74
        ORMPurger|string|null $purger,
75
        string $serviceName
76
    ): ?ORMPurger {
77
        if (null === $purger) {
78
            return null;
79
        }
80
81
        if (is_string($purger)) {
82
            $purger = $this->getService($container, $purger, $serviceName);
83
        }
84
85
        if (!$purger instanceof ORMPurger) {
86
            throw new ServiceNotCreatedException(
87
                sprintf(
88
                    'The \'purger\' must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
89
                    PurgerInterface::class,
90
                    is_object($purger) ? get_class($purger) : gettype($purger),
91
                    $serviceName
92
                )
93
            );
94
        }
95
96
        return $purger;
97
    }
98
}
99