Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

ClassMethodAnnotationReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 52
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A filter() 0 13 3
A read() 0 9 1
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Annotations;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\Annotations\AnnotationException;
16
17
/**
18
 * @author Yaroslav Honcharuk <[email protected]>
19
 */
20
class ClassMethodAnnotationReader implements ClassMethodAnnotationReaderInterface
21
{
22
    /**
23
     * @var Reader
24
     */
25
    private $delegate;
26
27
    /**
28
     * @param Reader|null $reader
29
     *
30
     * @throws AnnotationException
31
     */
32 3
    public function __construct(Reader $reader = null)
33
    {
34 3
        $this->delegate = $reader ?: new AnnotationReader();
35 3
    }
36
37
    /**
38
     * Note: Method annotation(s) doesn't replaces class annotation(s), but they are merged.
39
     *
40
     * {@inheritdoc}
41
     */
42 3
    public function read($class, $method, array $annotationClasses)
43
    {
44 3
        $object = new \ReflectionClass($class);
45 3
        $method = $object->getMethod($method);
46
47 3
        $classAnnotations = $this->filter($this->delegate->getClassAnnotations($object), $annotationClasses);
48 3
        $methodAnnotations = $this->filter($this->delegate->getMethodAnnotations($method), $annotationClasses);
49
50 3
        return array_merge($classAnnotations, $methodAnnotations);
51
    }
52
53
    /**
54
     * @param array $annotations
55
     * @param array $classes
56
     *
57
     * @return array Filtered annotations
58
     */
59 3
    private function filter(array $annotations, array $classes)
60
    {
61 3
        $filtered = [];
62
63 3
        foreach ($annotations as $annotation) {
64 3
            if (!in_array(get_class($annotation), $classes, true)) {
65 2
                continue;
66
            }
67
68 3
            $filtered[] = $annotation;
69
        }
70
71 3
        return $filtered;
72
    }
73
}
74