Failed Conditions
Pull Request — master (#62)
by Adrien
06:30 queued 04:20
created

MappingDriverChainAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 23
c 1
b 0
f 0
dl 0
loc 64
ccs 30
cts 30
cp 1
rs 10
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\Persistence\Mapping\Driver\AnnotationDriver;
9
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
10
use GraphQL\Doctrine\Exception;
11
use ReflectionClass;
12
use ReflectionMethod;
13
use ReflectionProperty;
14
15
final class MappingDriverChainAdapter implements Reader
16
{
17
    public function __construct(private readonly MappingDriverChain $chainDriver)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 17 at column 49
Loading history...
18
    {
19
    }
20
21
    /**
22
     * Find the reader for the class.
23
     */
24 9
    private function findReader(ReflectionClass $class): Reader
25
    {
26 9
        $className = $class->getName();
27 9
        foreach ($this->chainDriver->getDrivers() as $namespace => $driver) {
28 1
            if (mb_stripos($className, $namespace) === 0) {
29 1
                if ($driver instanceof AnnotationDriver) {
30 1
                    return $driver->getReader();
31
                }
32
            }
33
        }
34
35 8
        if ($this->chainDriver->getDefaultDriver() instanceof AnnotationDriver) {
36 7
            return $this->chainDriver->getDefaultDriver()->getReader();
37
        }
38
39 1
        throw new Exception('graphql-doctrine requires ' . $className . ' entity to be configured with a `' . AnnotationDriver::class . '`.');
40
    }
41
42 1
    public function getClassAnnotations(ReflectionClass $class)
43
    {
44 1
        return $this->findReader($class)
45
            ->getClassAnnotations($class);
46
    }
47
48 1
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
49
    {
50 1
        return $this->findReader($class)
51
            ->getClassAnnotation($class, $annotationName);
52
    }
53
54 2
    public function getMethodAnnotations(ReflectionMethod $method)
55
    {
56 2
        return $this->findReader($method->getDeclaringClass())
57
            ->getMethodAnnotations($method);
58
    }
59
60 3
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
61
    {
62 3
        return $this->findReader($method->getDeclaringClass())
63
            ->getMethodAnnotation($method, $annotationName);
64
    }
65
66 1
    public function getPropertyAnnotations(ReflectionProperty $property)
67
    {
68 1
        return $this->findReader($property->getDeclaringClass())
69
            ->getPropertyAnnotations($property);
70
    }
71
72 1
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
73
    {
74 1
        return $this->findReader($property->getDeclaringClass())
75
            ->getPropertyAnnotation($property, $annotationName);
76
    }
77
}
78