Passed
Pull Request — 12.2.x (#146)
by Tom
13:07
created

GlobalEnable::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 class-string[] $entityClasses */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string[].
Loading history...
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
    /** @param class-string $entityClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
62
    private function buildFieldMetadata(string $entityClass): void
63
    {
64
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
65
66
        foreach ($entityClassMetadata->getFieldNames() as $fieldName) {
67
            if (in_array($fieldName, $this->config->getIgnoreFields())) {
68
                continue;
69
            }
70
71
            $this->metadata[$entityClass]['fields'][$fieldName] = [
72
                'description' => $fieldName,
73
                'type' => $entityClassMetadata->getTypeOfField($fieldName),
74
                'hydratorStrategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)),
75
                'excludeFilters' => [],
76
            ];
77
        }
78
    }
79
80
    /** @param class-string $entityClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
81
    private function buildAssociationMetadata(string $entityClass): void
82
    {
83
        $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
84
85
        foreach ($entityClassMetadata->getAssociationNames() as $associationName) {
86
            if (in_array($associationName, $this->config->getIgnoreFields())) {
87
                continue;
88
            }
89
90
            $this->metadata[$entityClass]['fields'][$associationName] = [
91
                'limit' => null,
92
                'excludeFilters' => [],
93
                'description' => $associationName,
94
                'criteriaEventName' => null,
95
                'hydratorStrategy' => Strategy\AssociationDefault::class,
96
            ];
97
        }
98
    }
99
100
    protected function getConfig(): Config
101
    {
102
        return $this->config;
103
    }
104
}
105