Passed
Branch feature/configurable-annotatio... (1a9d97)
by Pieter
02:03
created

getClassAnnotations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
namespace W2w\Lib\Apie;
3
4
use Doctrine\Common\Annotations\Reader;
5
use ReflectionClass;
6
use ReflectionMethod;
7
use ReflectionProperty;
8
use W2w\Lib\Apie\Annotations\ApiResource;
9
10
/**
11
 * Extend the annotation reader with a config array to override the actual annotations if required. Should only
12
 * work on the ApiResource annotation.
13
 */
14
class ExtendReaderWithConfigReader implements Reader
15
{
16
    private $reader;
17
18
    private $config;
19
20
    /**
21
     * @param Reader $reader
22
     * @param ApiResource[] $config
23
     */
24
    public function __construct(Reader $reader, array $config)
25
    {
26
        $this->reader = $reader;
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function getClassAnnotations(ReflectionClass $class)
34
    {
35
        $className = $class->getName();
36
        $annotations = $this->reader->getClassAnnotations($class);
37
        if (isset($this->config[$className])) {
38
            $annotations = array_filter($annotations, function ($annotation) {
39
                return !($annotation instanceof ApiResource);
40
            });
41
            $annotations[] = $this->config[$className];
42
        }
43
44
        return $annotations;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
51
    {
52
        $className = $class->getName();
53
        if ($annotationName === ApiResource::class && isset($this->config[$className])) {
54
            return $this->config[$className];
55
        }
56
        return $this->reader->getClassAnnotation($class, $annotationName);
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function getMethodAnnotations(ReflectionMethod $method)
63
    {
64
        return $this->reader->getMethodAnnotations($method);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
71
    {
72
        return $this->reader->getMethodAnnotation($method, $annotationName);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function getPropertyAnnotations(ReflectionProperty $property)
79
    {
80
        return $this->reader->getPropertyAnnotations($property);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
87
    {
88
        return $this->reader->getPropertyAnnotation($property, $annotationName);
89
    }
90
}
91