ImportCommandFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
rs 9.8333
cc 2
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Console\Command;
6
7
use Arp\LaminasDoctrine\Console\Command\ImportCommand;
8
use Arp\LaminasFactory\AbstractFactory;
9
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
10
use Doctrine\Common\DataFixtures\Loader;
11
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\LaminasDoctrineFixtures\Factory\Command
19
 */
20
final class ImportCommandFactory extends AbstractFactory
21
{
22
    /**
23
     * @param ContainerInterface        $container
24
     * @param string                    $requestedName
25
     * @param array<string, mixed>|null $options
26
     *
27
     * @return ImportCommand
28
     *
29
     * @throws ServiceNotCreatedException
30
     * @throws ServiceNotFoundException
31
     */
32
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): ImportCommand
33
    {
34
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
35
36
        /** @var Loader $loader */
37
        $loader = $this->getService($container, $options['loader'] ?? Loader::class, $requestedName);
38
39
        try {
40
            $fixtures = $loader->getFixtures();
41
42
            return new ImportCommand(
43
                $fixtures,
44
                $this->getService($container, $options['executor'] ?? ORMExecutor::class, $requestedName),
45
                $this->getService($container, $options['purger'] ?? ORMPurger::class, $requestedName)
46
            );
47
        } catch (\Throwable $e) {
48
            throw new ServiceNotCreatedException(
49
                sprintf('Failed to load Doctrine fixtures: %s', $e->getMessage()),
50
                $e->getCode(),
51
                $e
52
            );
53
        }
54
    }
55
}
56