Passed
Pull Request — main (#124)
by Tom
02:55
created

GlobalEnable::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\GraphQL\Metadata;
6
7
use ApiSkeletons\Doctrine\GraphQL\Config;
8
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;
9
use Doctrine\ORM\EntityManager;
10
11
use function in_array;
12
13
final class GlobalEnable extends AbstractMetadataFactory
14
{
15
    /** @var mixed[] */
16
    private array $metadataConfig = [];
17
18
    public function __construct(
19
        private EntityManager $entityManager,
20
        protected Config $config,
21
    ) {
22
    }
23
24
    /**
25
     * @param string[] $entityClasses
26
     *
27
     * @return array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
28
     */
29
    public function __invoke(array $entityClasses): array
30
    {
31
        foreach ($entityClasses as $entityClass) {
32
            // Get extract by value or reference
33
            $byValue = $this->config->getGlobalByValue() ?? true;
34
35
            // Save entity-level metadata
36
            $this->metadataConfig[$entityClass] = [
37
                'entityClass' => $entityClass,
38
                'byValue' => $byValue,
39
                'namingStrategy' => null,
40
                'fields' => [],
41
                'filters' => [],
42
                'excludeCriteria' => [],
43
                'description' => $entityClass,
44
                'typeName' => $this->getTypeName($entityClass),
45
            ];
46
47
            $this->buildFieldMetadata($entityClass);
48
            $this->buildAssociationMetadata($entityClass);
49
        }
50
51
        return $this->metadataConfig;
52
    }
53
54
    private function buildFieldMetadata(string $entityClass): void
55
    {
56
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
57
58
        foreach ($entityClassMetadata->getFieldNames() as $fieldName) {
59
            if (in_array($fieldName, $this->config->getGlobalIgnore())) {
60
                continue;
61
            }
62
63
            $this->metadataConfig[$entityClass]['fields'][$fieldName] = [
64
                'description' => $fieldName,
65
                'type' => $entityClassMetadata->getTypeOfField($fieldName),
66
                'strategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)),
67
                'excludeCriteria' => [],
68
            ];
69
        }
70
    }
71
72
    private function buildAssociationMetadata(string $entityClass): void
73
    {
74
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
75
76
        foreach ($entityClassMetadata->getAssociationNames() as $associationName) {
77
            if (in_array($associationName, $this->config->getGlobalIgnore())) {
78
                continue;
79
            }
80
81
            $this->metadataConfig[$entityClass]['fields'][$associationName] = [
82
                'excludeCriteria' => [],
83
                'description' => $associationName,
84
                'filterCriteriaEventName' => null,
85
                'strategy' => Strategy\AssociationDefault::class,
86
            ];
87
        }
88
    }
89
}
90