Passed
Push — master ( b97c16...98e719 )
by Dawid
04:21
created

AnnotationMetaDataFactory::getMetaData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
cc 2
nc 2
nop 1
crap 2.0116
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\MetaData\Strategy;
4
5
use Cache\Adapter\PHPArray\ArrayCachePool;
6
use Doctrine\Common\Annotations\Annotation;
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\AnnotationRegistry;
9
use Doctrine\Common\Annotations\IndexedReader;
10
use Doctrine\Common\Annotations\Reader;
11
use Igni\Storage\Exception\MappingException;
12
use Igni\Storage\Mapping\Annotation\EmbeddedEntity;
13
use Igni\Storage\Mapping\Annotation\Entity;
14
use Igni\Storage\Mapping\Annotation\Property as Property;
15
use Igni\Storage\Mapping\MetaData\EntityMetaData;
16
use Igni\Storage\Mapping\MetaData\MetaDataFactory;
17
use Igni\Storage\Mapping\MetaData\PropertyMetaData;
18
use Igni\Storage\Mapping\Type;
19
use Igni\Utils\ReflectionApi;
20
use Psr\SimpleCache\CacheInterface;
21
use ReflectionProperty;
22
use ReflectionClass;
23
24
class AnnotationMetaDataFactory implements MetaDataFactory
25
{
26
    /**
27
     * @var CacheInterface
28
     */
29
    private $cache;
30
31
    /**
32
     * @var Reader
33
     */
34
    private $reader;
35
36 27
    public function __construct(CacheInterface $cache = null)
37
    {
38 27
        AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

38
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39 27
        $this->reader = new IndexedReader(new AnnotationReader());
40
41 27
        if ($cache === null) {
42 27
            $cache = new ArrayCachePool();
43
        }
44
45 27
        $this->cache = $cache;
46 27
    }
47
48 15
    public function getMetaData(string $entity): EntityMetaData
49
    {
50 15
        $cacheKey = str_replace('\\', '', $entity) . '.metadata';
51 15
        if ($this->cache->has($cacheKey)) {
52
            return $this->cache->get($cacheKey);
53
        }
54
55 15
        $metaData = $this->parseMetaData($entity);
56 15
        $this->cache->set($cacheKey, $metaData);
57
58 15
        return $metaData;
59
    }
60
61 15
    protected function parseMetaData(string $entityClass): EntityMetaData
62
    {
63 15
        $metaData = new EntityMetaData($entityClass);
64 15
        $reflection = ReflectionApi::reflectClass($entityClass);
65
66 15
        $this->parseClassAnnotations($reflection, $metaData);
67 15
        $this->parseProperties($reflection, $metaData);
68
69 15
        return $metaData;
70
    }
71
72 15
    private function parseClassAnnotations(ReflectionClass $reflection, EntityMetaData $metaData): void
73
    {
74 15
        $classAnnotations = $this->reader->getClassAnnotations($reflection);
75
76 15
        foreach ($classAnnotations as $type => $annotation) {
77
            switch ($type) {
78 15
                case Entity::class:
79 15
                    $source = $annotation->source ?? $annotation->value;
80 15
                    $metaData->setSource($source);
81 15
                    $this->setCustomHydrator($annotation, $metaData);
82 15
                    break;
83 2
                case EmbeddedEntity::class:
84 2
                    $metaData->makeEmbed();
85 2
                    $this->setCustomHydrator($annotation, $metaData);
86 15
                    break;
87
            }
88
        }
89 15
    }
90
91 15
    private function parseProperties(ReflectionClass $reflection, EntityMetaData $metaData): void
92
    {
93 15
        foreach ($reflection->getProperties() as $property) {
94 15
            $annotations = $this->reader->getPropertyAnnotations($property);
95 15
            foreach ($annotations as $annotation) {
96 15
                if ($annotation instanceof Property) {
97 15
                    $this->addProperty($property, $annotation, $metaData);
98 15
                    break;
99
                }
100
            }
101
        }
102
103 15
        if (!$metaData->definesProperties()) {
104
            throw MappingException::forEmptyMapping($metaData->getClass());
105
        }
106 15
    }
107
108 15
    private function setCustomHydrator(Annotation $annotation, EntityMetaData $metaData)
109
    {
110 15
        if ($annotation->hydrator !== null) {
111 14
            if (!class_exists($annotation->hydrator)) {
112
                throw new MappingException("Cannot use hydrator {$annotation->hydrator} class does not exist.");
113
            }
114
115 14
            $metaData->setCustomHydratorClass($annotation->hydrator);
116
        }
117 15
    }
118
119 15
    private function addProperty(ReflectionProperty $property, Property $annotation, EntityMetaData $metaData): void
120
    {
121 15
        if (!Type::has($annotation->getType())) {
122
            throw new MappingException("Cannot map property {$property->getDeclaringClass()->getName()}::{$property->getName()} - unknown type {$annotation->getType()}.");
123
        }
124
125 15
        $property = new PropertyMetaData(
126 15
            $property->getName(),
127 15
            Type::get($annotation->getType())
128
        );
129 15
        $property->setFieldName($annotation->name ?? $property->getName());
130 15
        $property->setAttributes($annotation->getAttributes());
131 15
        $metaData->addProperty($property);
132 15
    }
133
}
134