EntityHydratorFactory::__invoke()   B
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 42
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 42
rs 7.3166
cc 11
nc 9
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Hydrator;
6
7
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
8
use Arp\LaminasDoctrine\Hydrator\EntityHydrator;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Laminas\Hydrator\NamingStrategy\NamingStrategyEnabledInterface;
11
use Laminas\Hydrator\Strategy\StrategyEnabledInterface;
12
use Laminas\Hydrator\Strategy\StrategyInterface;
13
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
14
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\ContainerInterface;
17
use Psr\Container\NotFoundExceptionInterface;
18
19
/**
20
 * @author  Alex Patterson <[email protected]>
21
 * @package Arp\LaminasDoctrine\Factory\Hydrator
22
 */
23
final class EntityHydratorFactory extends AbstractFactory
24
{
25
    use EntityManagerFactoryProviderTrait;
26
27
    /**
28
     * @param ContainerInterface        $container
29
     * @param string                    $requestedName
30
     * @param array<string, mixed>|null $options
31
     *
32
     * @return EntityHydrator|object
33
     *
34
     * @throws ServiceNotCreatedException
35
     * @throws ServiceNotFoundException
36
     * @throws ContainerExceptionInterface
37
     * @throws NotFoundExceptionInterface
38
     */
39
    public function __invoke(ContainerInterface $container, string $requestedName, array $options = null)
40
    {
41
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'hydrators');
42
43
        $entityManager = $options['entity_manager'] ?? null;
44
        if (null === $entityManager) {
45
            throw new ServiceNotCreatedException(
46
                sprintf(
47
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
48
                    $requestedName
49
                )
50
            );
51
        }
52
53
        $inflector = isset($options['inflector'])
54
            ? $this->getService($container, $options['inflector'], $requestedName)
55
            : null;
56
57
        $hydrator = new EntityHydrator(
58
            $this->getEntityManager($container, $entityManager, $requestedName),
59
            !isset($options['by_value']) || $options['by_value'],
60
            $inflector
61
        );
62
63
        $namingStrategy = $options['naming_strategy'] ?? null;
64
        if (!empty($namingStrategy) && $hydrator instanceof NamingStrategyEnabledInterface) {
65
            $hydrator->setNamingStrategy($this->getService($container, $namingStrategy, $requestedName));
66
        }
67
68
        $strategies = $options['strategies'] ?? [];
69
        if (!empty($strategies) && $hydrator instanceof StrategyEnabledInterface) {
70
            foreach ($strategies as $name => $strategy) {
71
                if (is_string($strategy)) {
72
                    $strategy = $this->getService($container, $strategy, $requestedName);
73
                }
74
                if ($strategy instanceof StrategyInterface) {
75
                    $hydrator->addStrategy($name, $strategy);
76
                }
77
            }
78
        }
79
80
        return $hydrator;
81
    }
82
}
83