Passed
Pull Request — master (#47)
by Damien
03:44
created

AnnotationLoader::getEntityConfiguration()   F

Complexity

Conditions 18
Paths 390

Size

Total Lines 70
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
c 0
b 0
f 0
dl 0
loc 70
rs 1.6583
cc 18
nc 390
nop 1

How to fix   Long Method    Complexity   

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
namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Mapping\Entity;
8
use Doctrine\Persistence\Mapping\ClassMetadata;
9
10
class AnnotationLoader
11
{
12
    /**
13
     * @var null|AnnotationReader
14
     */
15
    private $reader;
16
17
    /**
18
     * @var EntityManagerInterface
19
     */
20
    private $entityManager;
21
22
    public function __construct(EntityManagerInterface $entityManager)
23
    {
24
        $this->entityManager = $entityManager;
25
        $this->reader = class_exists(AnnotationReader::class) ? new AnnotationReader() : null;
26
    }
27
28
    public function load(): array
29
    {
30
        $configuration = [];
31
32
        $metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();
33
        foreach ($metadatas as $metadata) {
34
            $config = $this->getEntityConfiguration($metadata);
35
            if (null !== $config) {
36
                $configuration[$metadata->getName()] = $config;
37
            }
38
        }
39
40
        return $configuration;
41
    }
42
43
    private function getEntityConfiguration(ClassMetadata $metadata): ?array
44
    {
45
        $annotation = null;
46
        $auditableAnnotation = null;
47
        $securityAnnotation = null;
48
        $annotationProperty = null;
49
        $reflection = $metadata->getReflectionClass();
50
51
        // Check that we have an Entity annotation
52
        if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Entity::class)) {
53
            $annotation = $attributes[0]->newInstance();
54
        } elseif (null !== $this->reader) {
55
            $annotation = $this->reader->getClassAnnotation($reflection, Entity::class);
56
        }
57
58
        if (null === $annotation) {
59
            return null;
60
        }
61
62
        // Check that we have an Auditable annotation
63
        if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Auditable::class)) {
64
            /** @var ?Auditable $auditableAnnotation */
65
            $auditableAnnotation = $attributes[0]->newInstance();
66
        } elseif (null !== $this->reader) {
67
            /** @var ?Auditable $auditableAnnotation */
68
            $auditableAnnotation = $this->reader->getClassAnnotation($reflection, Auditable::class);
69
        }
70
71
        if (null === $auditableAnnotation) {
72
            return null;
73
        }
74
75
        // Check that we have an Security annotation
76
        if (\PHP_VERSION_ID >= 80000 && $attributes = $reflection->getAttributes(Security::class)) {
77
            /** @var ?Security $securityAnnotation */
78
            $securityAnnotation = $attributes[0]->newInstance();
79
        } elseif (null !== $this->reader) {
80
            /** @var ?Security $securityAnnotation */
81
            $securityAnnotation = $this->reader->getClassAnnotation($reflection, Security::class);
82
        }
83
84
        if (null === $securityAnnotation) {
85
            $roles = null;
86
        } else {
87
            $roles = [
88
                Security::VIEW_SCOPE => $securityAnnotation->view,
89
            ];
90
        }
91
92
        $config = [
93
            'ignored_columns' => [],
94
            'enabled' => $auditableAnnotation->enabled,
95
            'roles' => $roles,
96
        ];
97
98
        // Are there any Ignore annotations?
99
        foreach ($reflection->getProperties() as $property) {
100
            if (\PHP_VERSION_ID >= 80000 && $attributes = $property->getAttributes(Ignore::class)) {
101
                $annotationProperty = $attributes[0]->newInstance();
102
            } elseif (null !== $this->reader) {
103
                $annotationProperty = $this->reader->getPropertyAnnotation($property, Ignore::class);
104
            }
105
106
            if (null !== $annotationProperty) {
107
                // TODO: $property->getName() might not be the column name
108
                $config['ignored_columns'][] = $property->getName();
109
            }
110
        }
111
112
        return $config;
113
    }
114
}
115