Passed
Branch feature/configurable-annotatio... (7bbf92)
by Pieter
03:21
created

ExtendReaderWithConfigReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethodAnnotations() 0 3 1
A getClassAnnotation() 0 7 3
A getMethodAnnotation() 0 3 1
A __construct() 0 4 1
A getClassAnnotations() 0 12 2
A getPropertyAnnotations() 0 3 1
A getPropertyAnnotation() 0 3 1
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
    function getClassAnnotations(ReflectionClass $class)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
    function getClassAnnotation(ReflectionClass $class, $annotationName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
    function getMethodAnnotations(ReflectionMethod $method)
63
    {
64
        return $this->reader->getMethodAnnotations($method);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    function getMethodAnnotation(ReflectionMethod $method, $annotationName)
71
    {
72
        return $this->reader->getMethodAnnotation($method, $annotationName);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    function getPropertyAnnotations(ReflectionProperty $property)
79
    {
80
        return $this->reader->getPropertyAnnotations($property);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
87
    {
88
        return $this->reader->getPropertyAnnotation($property, $annotationName);
89
    }
90
}
91