AnnotationLoader::getEntityConfiguration()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 43
rs 8.9617
1
<?php
2
3
namespace DH\DoctrineAuditBundle\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 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 = new AnnotationReader();
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public function load(): array
32
    {
33
        $configuration = [];
34
35
        $metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata();
36
        foreach ($metadatas as $metadata) {
37
            $config = $this->getEntityConfiguration($metadata);
38
            if (null !== $config) {
39
                $configuration[$metadata->getName()] = $config;
40
            }
41
        }
42
43
        return $configuration;
44
    }
45
46
    /**
47
     * @param ClassMetadata $metadata
48
     *
49
     * @return null|array
50
     */
51
    private function getEntityConfiguration(ClassMetadata $metadata): ?array
52
    {
53
        $reflection = $metadata->getReflectionClass();
54
55
        // Check that we have an Entity annotation
56
        $annotation = $this->reader->getClassAnnotation($reflection, Entity::class);
57
        if (null === $annotation) {
58
            return null;
59
        }
60
61
        // Check that we have an Auditable annotation
62
        /** @var ?Auditable $auditableAnnotation */
63
        $auditableAnnotation = $this->reader->getClassAnnotation($reflection, Auditable::class);
64
        if (null === $auditableAnnotation) {
65
            return null;
66
        }
67
68
        // Check that we have an Security annotation
69
        /** @var ?Security $securityAnnotation */
70
        $securityAnnotation = $this->reader->getClassAnnotation($reflection, Security::class);
71
        if (null === $securityAnnotation) {
72
            $roles = null;
73
        } else {
74
            $roles = [
75
                Security::VIEW_SCOPE => $securityAnnotation->view,
76
            ];
77
        }
78
79
        $config = [
80
            'ignored_columns' => [],
81
            'enabled' => $auditableAnnotation->enabled,
82
            'roles' => $roles,
83
        ];
84
85
        // Are there any Ignore annotations?
86
        foreach ($reflection->getProperties() as $property) {
87
            if ($this->reader->getPropertyAnnotation($property, Ignore::class)) {
88
                // TODO: $property->getName() might not be the column name
89
                $config['ignored_columns'][] = $property->getName();
90
            }
91
        }
92
93
        return $config;
94
    }
95
}
96