Completed
Push — master ( ff238a...ae4d18 )
by Vitaly
03:13 queued 01:01
created

AnnotationResolver::resolve()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 8
Bugs 0 Features 6
Metric Value
c 8
b 0
f 6
dl 0
loc 19
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
crap 3.0123
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 29.07.2016
6
 * Time: 21:38.
7
 */
8
namespace samsonframework\container\resolver;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use samsonframework\container\annotation\MetadataInterface;
12
use samsonframework\container\annotation\MethodAnnotation;
13
use samsonframework\container\metadata\ClassMetadata;
14
use samsonframework\container\metadata\MethodMetadata;
15
16
/**
17
 * Annotation resolver class.
18
 */
19
class AnnotationResolver extends Resolver
20
{
21
    /**
22
     * @var AnnotationReader
23
     */
24
    protected $reader;
25
26
    /**
27
     * AnnotationResolver constructor.
28
     *
29
     * @throws \InvalidArgumentException
30
     */
31 1
    public function __construct()
32
    {
33 1
        $this->reader = new AnnotationReader();
34 1
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 1
    public function resolve($classData, $identifier = null)
40
    {
41
        /** @var \ReflectionClass $classData */
42
43
        // Create and fill class metadata base fields
44 1
        $metadata = new ClassMetadata();
45 1
        $metadata->className = $classData->getName();
46 1
        $metadata->internalId = $identifier ?: uniqid();
47 1
        $metadata->name = $metadata->internalId;
48
49 1
        $this->resolveClassAnnotations($classData, $metadata);
50
51
        /** @var \ReflectionMethod $method */
52 1
        foreach ($classData->getMethods() as $method) {
53
            $this->resolveMethodAnnotation($method, $metadata);
54
        }
55
56 1
        return $metadata;
57
    }
58
59
    /**
60
     * Resolve all class annotations.
61
     *
62
     * @param \ReflectionClass $classData
63
     * @param ClassMetadata    $metadata
64
     */
65 1
    protected function resolveClassAnnotations(\ReflectionClass $classData, ClassMetadata $metadata)
66
    {
67
        /** @var MetadataInterface $annotation Read class annotations */
68 1
        foreach ($this->reader->getClassAnnotations($classData) as $annotation) {
69 1
            if (class_implements($annotation, MetadataInterface::class)) {
70 1
                $annotation->toMetadata($metadata);
71
            }
72
        }
73 1
    }
74
75
    /**
76
     * Resolve all method annotations.
77
     *
78
     * @param \ReflectionMethod $method
79
     * @param ClassMetadata     $metadata
80
     */
81
    protected function resolveMethodAnnotation(\ReflectionMethod $method, ClassMetadata $metadata)
82
    {
83
        $methodAnnotations = $this->reader->getMethodAnnotations($method);
84
        $methodMetadata = new MethodMetadata();
85
        $methodMetadata->name = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
86
        $methodMetadata->modifiers = $method->getModifiers();
87
        $methodMetadata->parameters = $method->getParameters();
88
89
        /** @var MethodAnnotation $methodAnnotation */
90
        foreach ($methodAnnotations as $methodAnnotation) {
91
            $methodMetadata->options[$methodAnnotation->getMethodAlias()] = $methodAnnotation->convertToMetadata();
92
        }
93
        $metadata->methodsMetadata[$method->getName()] = $methodMetadata;
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
94
    }
95
}
96