HydratorStrategyFactory::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 42
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 42
rs 9.6
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Hydrator\Strategy;
6
7
use Arp\Entity\EntityInterface;
8
use Arp\LaminasDoctrine\Hydrator\Strategy\HydratorStrategy;
9
use Arp\LaminasDoctrine\Repository\EntityRepositoryInterface;
10
use Arp\LaminasDoctrine\Repository\RepositoryManager;
11
use Arp\LaminasFactory\AbstractFactory;
12
use Laminas\Hydrator\HydratorPluginManager;
13
use Laminas\Hydrator\Strategy\Exception\InvalidArgumentException;
14
use Laminas\Hydrator\Strategy\HydratorStrategy as LaminasHydratorStrategy;
15
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
16
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
17
use Psr\Container\ContainerExceptionInterface;
18
use Psr\Container\ContainerInterface;
19
20
final class HydratorStrategyFactory extends AbstractFactory
21
{
22
    /**
23
     * @param ContainerInterface        $container
24
     * @param string                    $requestedName
25
     * @param array<string, mixed>|null $options
26
     *
27
     * @return HydratorStrategy
28
     *
29
     * @throws ServiceNotCreatedException
30
     * @throws ServiceNotFoundException
31
     * @throws InvalidArgumentException
32
     * @throws ContainerExceptionInterface
33
     */
34
    public function __invoke(
35
        ContainerInterface $container,
36
        string $requestedName,
37
        array $options = null
38
    ): HydratorStrategy {
39
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
40
41
        $entityName = $options['entity_name'] ?? null;
42
        if (null === $entityName) {
43
            throw new ServiceNotCreatedException(
44
                sprintf(
45
                    'The required \'entity_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
        /** @var HydratorPluginManager $hydratorManager */
62
        $hydratorManager = $this->getService($container, 'HydratorManager', $requestedName);
63
64
        $laminasHydrator = new LaminasHydratorStrategy(
65
            $this->getService($hydratorManager, $hydrator, $requestedName),
0 ignored issues
show
Bug introduced by
It seems like $this->getService($hydra...drator, $requestedName) can also be of type array; however, parameter $objectHydrator of Laminas\Hydrator\Strateg...Strategy::__construct() does only seem to accept Laminas\Hydrator\HydratorInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            /** @scrutinizer ignore-type */ $this->getService($hydratorManager, $hydrator, $requestedName),
Loading history...
66
            $entityName
67
        );
68
69
        /** @var RepositoryManager $repositoryManager */
70
        $repositoryManager = $this->getService($container, RepositoryManager::class, $requestedName);
71
72
        /** @var EntityRepositoryInterface<EntityInterface> $repository */
73
        $repository = $this->getService($repositoryManager, $entityName, $requestedName);
74
75
        return new HydratorStrategy($repository, $laminasHydrator);
76
    }
77
}
78