OrmPurgerFactory::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 9.7998
cc 3
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\DataFixture;
6
7
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManager\ObjectManagerArgvInputProviderTrait;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * @author  Alex Patterson <[email protected]>
17
 * @package Arp\LaminasDoctrine\Factory\Data
18
 */
19
final class OrmPurgerFactory extends AbstractFactory
20
{
21
    use ObjectManagerArgvInputProviderTrait;
22
    use EntityManagerFactoryProviderTrait;
23
24
    /**
25
     * @param ContainerInterface        $container
26
     * @param string                    $requestedName
27
     * @param array<string, mixed>|null $options
28
     *
29
     * @return ORMPurger
30
     *
31
     * @throws ServiceNotCreatedException
32
     * @throws ServiceNotFoundException
33
     */
34
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): ORMPurger
35
    {
36
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
37
38
        $entityManagerName = $options['entity_manager'] ?? null;
39
        if (null === $entityManagerName) {
40
            $entityManagerName = $this->getEntityManagerArgvInput();
41
        }
42
43
        if (empty($entityManagerName)) {
44
            throw new ServiceNotCreatedException(
45
                sprintf(
46
                    'The \'entity_manager\' configuration option could not be found for service \'%s\'',
47
                    $requestedName
48
                )
49
            );
50
        }
51
52
        $purger = new ORMPurger(
53
            $this->getEntityManager($container, $entityManagerName, $requestedName),
54
            $options['excluded_table_names'] ?? []
55
        );
56
57
        $purger->setPurgeMode($options['mode'] ?? ORMPurger::PURGE_MODE_DELETE);
58
59
        return $purger;
60
    }
61
}
62