Completed
Pull Request — master (#19)
by James
02:53
created

MappingDriverChainAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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