HydratorContainer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 30 4
A __construct() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator;
6
7
use ApiSkeletons\Doctrine\ORM\GraphQL\Container;
8
use ApiSkeletons\Doctrine\ORM\GraphQL\Type\Entity\EntityTypeContainer;
9
use Doctrine\Laminas\Hydrator\DoctrineObject;
10
use Doctrine\ORM\EntityManager;
11
use GraphQL\Error\Error;
12
use Laminas\Hydrator\NamingStrategy\MapNamingStrategy;
13
use Laminas\Hydrator\Strategy\StrategyInterface;
14
15
use function assert;
16
use function class_implements;
17
use function in_array;
18
19
/**
20
 * This factory is used in the Metadata\Entity class to create a hydrator
21
 * for the current entity
22
 */
23
class HydratorContainer extends Container
24
{
25
    public function __construct(
26
        protected readonly EntityManager $entityManager,
27
        protected readonly EntityTypeContainer $entityTypeContainer,
28
    ) {
29
        // Register default strategies
30
        $this
31
            ->set(Strategy\AssociationDefault::class, static fn () => new Strategy\AssociationDefault())
32
            ->set(Strategy\FieldDefault::class, static fn () => new Strategy\FieldDefault())
33
            ->set(Strategy\ToBoolean::class, static fn () => new Strategy\ToBoolean())
34
            ->set(Strategy\ToFloat::class, static fn () => new Strategy\ToFloat())
35
            ->set(Strategy\ToInteger::class, static fn () => new Strategy\ToInteger());
36
    }
37
38
    /** @throws Error */
39
    public function get(string $id): mixed
40
    {
41
        if ($this->has($id)) {
42
            return parent::get($id);
43
        }
44
45
        $entity   = $this->entityTypeContainer->get($id);
46
        $metadata = $entity->getMetadata();
47
        $hydrator = new DoctrineObject($this->entityManager, $metadata['byValue']);
48
49
        // Create field strategy and assign to hydrator
50
        foreach ($metadata['fields'] as $fieldName => $fieldMetadata) {
51
            assert(
52
                in_array(StrategyInterface::class, class_implements($fieldMetadata['hydratorStrategy'])),
53
                'Strategy must implement ' . StrategyInterface::class,
54
            );
55
56
            $hydrator->addStrategy($fieldName, $this->get($fieldMetadata['hydratorStrategy']));
57
        }
58
59
        // Create naming strategy for aliases and assign to hydrator
60
        if ($entity->getExtractionMap()) {
61
            $hydrator->setNamingStrategy(
62
                MapNamingStrategy::createFromExtractionMap($entity->getExtractionMap()),
63
            );
64
        }
65
66
        $this->set($id, $hydrator);
67
68
        return $hydrator;
69
    }
70
}
71