HydratorCollectionStrategyFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 47
rs 9.52
cc 4
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Hydrator\Strategy;
6
7
use Arp\LaminasDoctrine\Hydrator\Strategy\HydratorCollectionStrategy;
8
use Arp\LaminasDoctrine\Repository\RepositoryManager;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Laminas\Hydrator\HydratorPluginManager;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\LaminasDoctrine\Factory\Hydrator\Strategy
19
 */
20
final class HydratorCollectionStrategyFactory extends AbstractFactory
21
{
22
    /**
23
     * @param ContainerInterface        $container
24
     * @param string                    $requestedName
25
     * @param array<string, mixed>|null $options
26
     *
27
     * @return HydratorCollectionStrategy
28
     *
29
     * @throws ServiceNotCreatedException
30
     * @throws ServiceNotFoundException
31
     * @throws ContainerExceptionInterface
32
     */
33
    public function __invoke(
34
        ContainerInterface $container,
35
        string $requestedName,
36
        array $options = null
37
    ): HydratorCollectionStrategy {
38
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
39
40
        $entityName = $options['entity_name'] ?? null;
41
        if (empty($entityName)) {
42
            throw new ServiceNotCreatedException(
43
                sprintf(
44
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
45
                    $requestedName
46
                )
47
            );
48
        }
49
50
        $fieldName = $options['field_name'] ?? null;
51
        if (empty($fieldName)) {
52
            throw new ServiceNotCreatedException(
53
                sprintf(
54
                    'The required \'field_name\' configuration option is missing for service \'%s\'',
55
                    $requestedName
56
                )
57
            );
58
        }
59
60
        $hydrator = $options['hydrator'] ?? null;
61
        if (empty($hydrator)) {
62
            throw new ServiceNotCreatedException(
63
                sprintf(
64
                    'The required \'hydrator\' configuration option is missing for service \'%s\'',
65
                    $requestedName
66
                )
67
            );
68
        }
69
70
        /** @var RepositoryManager $repositoryManager */
71
        $repositoryManager = $this->getService($container, RepositoryManager::class, $requestedName);
72
73
        /** @var HydratorPluginManager $hydratorManager */
74
        $hydratorManager = $this->getService($container, 'HydratorManager', $requestedName);
75
76
        return new HydratorCollectionStrategy(
77
            $fieldName,
78
            $this->getService($repositoryManager, $entityName, $requestedName),
79
            $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 $hydrator of Arp\LaminasDoctrine\Hydr...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

79
            /** @scrutinizer ignore-type */ $this->getService($hydratorManager, $hydrator, $requestedName)
Loading history...
80
        );
81
    }
82
}
83