Completed
Push — master ( 83d554...edb865 )
by Alexander
06:18 queued 04:13
created

AnnotationParser::parseMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Flying\Struct\Metadata;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Doctrine\Common\Annotations\Reader;
8
use Flying\Struct\Annotation\Annotation;
9
use Flying\Struct\Annotation\Property;
10
use Flying\Struct\Annotation\Struct;
11
use Flying\Struct\ConfigurationManager;
12
use Flying\Struct\Exception;
13
14
/**
15
 * Structures metadata parser implementation for parsing structure annotations
16
 */
17
class AnnotationParser extends AbstractMetadataParser
18
{
19
    /**
20
     * Annotations reader
21
     *
22
     * @var Reader
23
     */
24
    private $reader;
25
26
    /**
27
     * {@inheritdoc}
28
     * @throws \Flying\Struct\Exception
29
     * @throws \InvalidArgumentException
30
     */
31 182
    protected function parseMetadata(\ReflectionClass $reflection, StructMetadata $metadata)
32
    {
33 182
        $reader = $this->getReader();
34 182
        $annotations = $reader->getClassAnnotations($reflection);
35 182
        foreach ($annotations as $annotation) {
36 181
            $metadata->addProperty($this->convertToMetadata($annotation));
37 182
        }
38 181
    }
39
40
    /**
41
     * Get annotations reader
42
     *
43
     * @return Reader
44
     * @throws \InvalidArgumentException
45
     */
46 182
    public function getReader()
47
    {
48 182
        if (!$this->reader) {
49 182
            $this->reader = new AnnotationReader();
50 182
            AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

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

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

Loading history...
51 182
        }
52 182
        return $this->reader;
53
    }
54
55
    /**
56
     * Set annotations reader to use for parsing structure annotations
57
     *
58
     * @param Reader $reader
59
     *
60
     * @return $this
61
     */
62 1
    public function setReader(Reader $reader)
63
    {
64 1
        $this->reader = $reader;
65 1
        return $this;
66
    }
67
68
    /**
69
     * Convert given structure annotation into structure metadata
70
     *
71
     * @param Annotation $annotation Structure annotation to convert
72
     *
73
     * @return PropertyMetadata
74
     * @throws \InvalidArgumentException
75
     * @throws Exception
76
     */
77 181
    protected function convertToMetadata(Annotation $annotation)
78
    {
79 181
        if ($annotation instanceof Property) {
80 179
            $class = $this->resolvePropertyClass($annotation->getType());
81 179
            if (!is_string($class)) {
82 1
                throw new Exception('Unable to resolve structure property class for type: ' . $annotation->getType());
83
            }
84 178
            $property = new PropertyMetadata($annotation->getName(), $class, $annotation->getConfig());
85 178
            return $property;
86
        }
87
88 67
        if (!($annotation instanceof Struct)) {
89
            throw new Exception('Unknown structure annotation type');
90
        }
91
92 67
        if ($annotation->getClass()) {
93 67
            $class = $this->resolveStructClass($annotation->getClass());
94 67
            if (!is_string($class)) {
95 2
                throw new Exception('Unable to resolve structure class: ' . $annotation->getClass());
96
            }
97 65
            $struct = ConfigurationManager::getConfiguration()->getMetadataManager()->getMetadata($class);
98 65
            if (!$struct instanceof StructMetadata) {
99 1
                throw new Exception('Failed to get structure metadata for class: ' . $class);
100
            }
101 64
            $struct->setName($annotation->getName());
102 64
            $struct->setConfig($annotation->getConfig());
103 64
        } else {
104 15
            if (!count($annotation->getProperties())) {
105
                throw new Exception('Structure metadata should have either class name or explicitly defined list of structure properties');
106
            }
107 15
            $struct = new StructMetadata($annotation->getName(), null, $annotation->getConfig());
108 15
            foreach ($annotation->getProperties() as $p) {
109 15
                $struct->addProperty($this->convertToMetadata($p));
110 15
            }
111
        }
112 64
        return $struct;
113
    }
114
}
115