Passed
Push — master ( 5010f8...422826 )
by Damien
02:59
created

AnnotationLoader::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Annotation;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\Entity;
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
    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
        $reflection = $metadata->getReflectionClass();
46
47
        // Check that we have an Entity annotation
48
        $annotation = $this->reader->getClassAnnotation($reflection, Entity::class);
49
        if (null === $annotation) {
50
            return null;
51
        }
52
53
        // Check that we have an Auditable annotation
54
        $auditableAnnotation = $this->reader->getClassAnnotation($reflection, Auditable::class);
55
        if (null === $auditableAnnotation) {
56
            return null;
57
        }
58
59
        // Check that we have an Security annotation
60
        $securityAnnotation = $this->reader->getClassAnnotation($reflection, Security::class);
61
        if (null === $securityAnnotation) {
62
            $roles = null;
63
        } else {
64
            $roles = [
65
                Security::VIEW_SCOPE => $securityAnnotation->view
66
            ];
67
        }
68
69
        $config = [
70
            'ignored_columns' => [],
71
            'enabled' => $auditableAnnotation->enabled,
72
            'roles' => $roles,
73
        ];
74
75
        // Are there any Ignore annotations?
76
        foreach ($reflection->getProperties() as $property) {
77
            if ($this->reader->getPropertyAnnotation($property, Ignore::class)) {
78
                // TODO: $property->getName() might not be the column name
79
                $config['ignored_columns'][] = $property->getName();
80
            }
81
        }
82
83
        return $config;
84
    }
85
}
86