HydratorFixtureFactory::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 48
rs 8.8817
cc 6
nc 6
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Data;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Doctrine\Common\DataFixtures\FixtureInterface;
9
use Laminas\Hydrator\HydratorInterface;
10
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
11
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
12
use Psr\Container\ContainerExceptionInterface;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * Factory class that will construct a class that extends AbstractHydratorFixture based on configuration options.
17
 *
18
 * @author  Alex Patterson <[email protected]>
19
 * @package Arp\LaminasDoctrine\Factory\Data
20
 */
21
final class HydratorFixtureFactory extends AbstractFactory
22
{
23
    /**
24
     * @param ContainerInterface        $container
25
     * @param string                    $requestedName
26
     * @param array<string, mixed>|null $options
27
     *
28
     * @return FixtureInterface
29
     *
30
     * @throws ServiceNotCreatedException
31
     * @throws ServiceNotFoundException
32
     * @throws ContainerExceptionInterface
33
     */
34
    public function __invoke(
35
        ContainerInterface $container,
36
        string $requestedName,
37
        array $options = null
38
    ): FixtureInterface {
39
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'data_fixtures');
40
41
        $className = $options['class_name'] ?? null;
42
        if (null === $className) {
43
            throw new ServiceNotCreatedException(
44
                sprintf(
45
                    'The required \'class_name\' configuration option is missing for service \'%s\'',
46
                    $requestedName
47
                )
48
            );
49
        }
50
51
        $hydrator = $options['hydrator'] ?? null;
52
        if (null === $hydrator) {
53
            throw new ServiceNotCreatedException(
54
                sprintf(
55
                    'The required \'hydrator\' configuration option is missing for service \'%s\'',
56
                    $requestedName
57
                )
58
            );
59
        }
60
61
        if (is_string($hydrator)) {
62
            $hydrator = $this->getService(
63
                $this->getService($container, 'HydratorManager', $requestedName),
64
                $hydrator,
65
                $requestedName
66
            );
67
        }
68
69
        if (!$hydrator instanceof HydratorInterface) {
70
            throw new ServiceNotCreatedException(
71
                sprintf(
72
                    'The \'hydrator\' must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
73
                    HydratorInterface::class,
74
                    is_object($hydrator) ? get_class($hydrator) : gettype($hydrator),
75
                    $requestedName
76
                )
77
            );
78
        }
79
80
        /** @var class-string<FixtureInterface> $className */
81
        return new $className($hydrator, $options['data'] ?? []);
82
    }
83
}
84