Passed
Pull Request — master (#19)
by James
05:53
created

MappingDriverChainAdapter::findReader()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

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