Completed
Push — master ( f0c19b...798d36 )
by Adrien
03:53
created

MappingDriverChainAdapter::getPropertyAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Factory\MetadataReader;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver;
9
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
10
use GraphQL\Doctrine\Exception;
11
use ReflectionClass;
12
13
class MappingDriverChainAdapter implements Reader
14
{
15
    /**
16
     * @var MappingDriverChain
17
     */
18
    private $chainDriver;
19
20 9
    public function __construct(MappingDriverChain $chainDriver)
21
    {
22 9
        $this->chainDriver = $chainDriver;
23 9
    }
24
25
    /**
26
     * Find the reader for the class
27
     *
28
     * @param ReflectionClass $class
29
     *
30
     * @return Reader
31
     */
32 9
    private function findReader(ReflectionClass $class): Reader
33
    {
34 9
        $className = $class->getName();
35 9
        foreach ($this->chainDriver->getDrivers() as $namespace => $driver) {
36 1
            if (mb_stripos($className, $namespace) === 0) {
37 1
                if ($driver instanceof AnnotationDriver) {
38 1
                    return $driver->getReader();
39
                }
40
            }
41
        }
42
43 8
        if ($this->chainDriver->getDefaultDriver() instanceof AnnotationDriver) {
44 7
            return $this->chainDriver->getDefaultDriver()->getReader();
45
        }
46
47 1
        throw new Exception('graphql-doctrine requires ' . $className . ' entity to be configured with a `' . AnnotationDriver::class . '`.');
48
    }
49
50
    /**
51
     * Gets the annotations applied to a class.
52
     *
53
     * @param ReflectionClass $class the ReflectionClass of the class from which
54
     *                                the class annotations should be read
55
     *
56
     * @return array an array of Annotations
57
     */
58 1
    public function getClassAnnotations(ReflectionClass $class)
59
    {
60 1
        return $this->findReader($class)
61 1
            ->getClassAnnotations($class);
62
    }
63
64
    /**
65
     * Gets a class annotation.
66
     *
67
     * @param ReflectionClass $class the ReflectionClass of the class from which
68
     *                                         the class annotations should be read
69
     * @param string $annotationName the name of the annotation
70
     *
71
     * @return null|object the Annotation or NULL, if the requested annotation does not exist
72
     */
73 1
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
74
    {
75 1
        return $this->findReader($class)
76 1
            ->getClassAnnotation($class, $annotationName);
77
    }
78
79
    /**
80
     * Gets the annotations applied to a method.
81
     *
82
     * @param \ReflectionMethod $method the ReflectionMethod of the method from which
83
     *                                  the annotations should be read
84
     *
85
     * @return array an array of Annotations
86
     */
87 2
    public function getMethodAnnotations(\ReflectionMethod $method)
88
    {
89 2
        return $this->findReader($method->getDeclaringClass())
90 2
            ->getMethodAnnotations($method);
91
    }
92
93
    /**
94
     * Gets a method annotation.
95
     *
96
     * @param \ReflectionMethod $method the ReflectionMethod to read the annotations from
97
     * @param string $annotationName the name of the annotation
98
     *
99
     * @return null|object the Annotation or NULL, if the requested annotation does not exist
100
     */
101 3
    public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
102
    {
103 3
        return $this->findReader($method->getDeclaringClass())
104 2
            ->getMethodAnnotation($method, $annotationName);
105
    }
106
107
    /**
108
     * Gets the annotations applied to a property.
109
     *
110
     * @param \ReflectionProperty $property the ReflectionProperty of the property
111
     *                                      from which the annotations should be read
112
     *
113
     * @return array an array of Annotations
114
     */
115 1
    public function getPropertyAnnotations(\ReflectionProperty $property)
116
    {
117 1
        return $this->findReader($property->getDeclaringClass())
118 1
            ->getPropertyAnnotations($property);
119
    }
120
121
    /**
122
     * Gets a property annotation.
123
     *
124
     * @param \ReflectionProperty $property the ReflectionProperty to read the annotations from
125
     * @param string $annotationName the name of the annotation
126
     *
127
     * @return null|object the Annotation or NULL, if the requested annotation does not exist
128
     */
129 1
    public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
130
    {
131 1
        return $this->findReader($property->getDeclaringClass())
132 1
            ->getPropertyAnnotation($property, $annotationName);
133
    }
134
}
135