GlobalEnable::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL\Metadata;
6
7
use ApiSkeletons\Doctrine\ORM\GraphQL\Config;
8
use ApiSkeletons\Doctrine\ORM\GraphQL\Event\Metadata;
9
use ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\Strategy;
10
use ApiSkeletons\Doctrine\ORM\GraphQL\Metadata\Common\MetadataFactory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiSkeletons\Doctrine\OR...etadata\MetadataFactory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use ArrayObject;
12
use Doctrine\ORM\EntityManager;
13
use League\Event\EventDispatcher;
14
15
use function in_array;
16
17
/**
18
 * Build metadata for all entities
19
 */
20
final class GlobalEnable extends MetadataFactory
21
{
22
    private ArrayObject $metadata;
23
24
    public function __construct(
25
        protected readonly EntityManager $entityManager,
26
        protected readonly Config $config,
27
        protected readonly EventDispatcher $eventDispatcher,
28
    ) {
29
        $this->metadata = new ArrayObject();
30
    }
31
32
    /** @param string[] $entityClasses */
33
    public function __invoke(array $entityClasses): ArrayObject
34
    {
35
        foreach ($entityClasses as $entityClass) {
36
            // Get extract by value or reference
37
            $byValue = $this->config->getGlobalByValue() ?? true;
38
39
            // Save entity-level metadata
40
            $this->metadata[$entityClass] = [
41
                'entityClass' => $entityClass,
42
                'byValue' => $byValue,
43
                'limit' => 0,
44
                'fields' => [],
45
                'excludeFilters' => [],
46
                'description' => $entityClass,
47
                'typeName' => $this->getTypeName($entityClass),
48
            ];
49
50
            $this->buildFieldMetadata($entityClass);
51
            $this->buildAssociationMetadata($entityClass);
52
        }
53
54
        $this->eventDispatcher->dispatch(
55
            new Metadata($this->metadata, 'metadata.build'),
56
        );
57
58
        return $this->metadata;
59
    }
60
61
    private function buildFieldMetadata(string $entityClass): void
62
    {
63
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
64
65
        foreach ($entityClassMetadata->getFieldNames() as $fieldName) {
66
            if (in_array($fieldName, $this->config->getIgnoreFields())) {
67
                continue;
68
            }
69
70
            $this->metadata[$entityClass]['fields'][$fieldName] = [
71
                'description' => $fieldName,
72
                'type' => $entityClassMetadata->getTypeOfField($fieldName),
73
                'hydratorStrategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)),
74
                'excludeFilters' => [],
75
            ];
76
        }
77
    }
78
79
    private function buildAssociationMetadata(string $entityClass): void
80
    {
81
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
82
83
        foreach ($entityClassMetadata->getAssociationNames() as $associationName) {
84
            if (in_array($associationName, $this->config->getIgnoreFields())) {
85
                continue;
86
            }
87
88
            $this->metadata[$entityClass]['fields'][$associationName] = [
89
                'limit' => null,
90
                'excludeFilters' => [],
91
                'description' => $associationName,
92
                'criteriaEventName' => null,
93
                'hydratorStrategy' => Strategy\AssociationDefault::class,
94
            ];
95
        }
96
    }
97
}
98