Passed
Push — main ( d0cb76...ea9e69 )
by Tom
01:11 queued 12s
created

GlobalEnable::__invoke()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 63
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 36
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 63
rs 8.7217

How to fix   Long Method   

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 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 mixed[]
28
     */
29
    public function __invoke(array $entityClasses): array
30
    {
31
        $globalIgnore = $this->config->getGlobalIgnore();
32
33
        foreach ($entityClasses as $entityClass) {
34
            // Get extract by value or reference
35
            $byValue = $this->config->getGlobalByValue() ?? true;
36
37
            // Save entity-level metadata
38
            $this->metadataConfig[$entityClass] = [
39
                'entityClass' => $entityClass,
40
                'byValue' => $byValue,
41
                'namingStrategy' => null,
42
                'fields' => [],
43
                'filters' => [],
44
                'excludeCriteria' => [],
45
                'description' => $entityClass,
46
                'typeName' => $this->getTypeName($entityClass),
47
            ];
48
49
            // Fetch fields
50
            $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
51
            $fieldNames          = $entityClassMetadata->getFieldNames();
52
53
            foreach ($fieldNames as $fieldName) {
54
                if (in_array($fieldName, $globalIgnore)) {
55
                    continue;
56
                }
57
58
                $this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] =
59
                    $fieldName;
60
61
                $this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] =
62
                    $entityClassMetadata->getTypeOfField($fieldName);
63
64
                // Set default strategy based on field type
65
                $this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] =
66
                    $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));
67
68
                $this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
69
            }
70
71
            // Fetch attributes for associations
72
            $associationNames = $this->entityManager->getMetadataFactory()
73
                ->getMetadataFor($entityClass)->getAssociationNames();
74
75
            foreach ($associationNames as $associationName) {
76
                if (in_array($associationName, $globalIgnore)) {
77
                    continue;
78
                }
79
80
                $this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria']         = [];
81
                $this->metadataConfig[$entityClass]['fields'][$associationName]['description']             = $associationName;
82
                $this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
83
                    = null;
84
85
                // NullifyOwningAssociation is not used for globalEnable
86
                $this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
87
                    Strategy\AssociationDefault::class;
88
            }
89
        }
90
91
        return $this->metadataConfig;
92
    }
93
}
94