Completed
Pull Request — master (#44)
by David
01:58
created

AnnotationReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
7
use Doctrine\Common\Annotations\Reader;
8
use ReflectionClass;
9
use ReflectionMethod;
10
use TheCodingMachine\GraphQL\Controllers\Annotations\AbstractRequest;
11
use TheCodingMachine\GraphQL\Controllers\Annotations\Exceptions\ClassNotFoundException;
12
use TheCodingMachine\GraphQL\Controllers\Annotations\Logged;
13
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
14
use TheCodingMachine\GraphQL\Controllers\Annotations\SourceField;
15
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
16
17
class AnnotationReader
18
{
19
    /**
20
     * @var Reader
21
     */
22
    private $reader;
23
24
    public function __construct(Reader $reader)
25
    {
26
        $this->reader = $reader;
27
    }
28
29
    public function getTypeAnnotation(ReflectionClass $refClass): ?Type
30
    {
31
        try {
32
            /** @var Type|null $typeField */
33
            $typeField = $this->getClassAnnotation($refClass, Type::class);
34
        } catch (ClassNotFoundException $e) {
35
            throw ClassNotFoundException::wrapException($e, $refClass->getName());
36
        }
37
        return $typeField;
38
    }
39
40
    public function getRequestAnnotation(ReflectionMethod $refMethod, string $annotationName): ?AbstractRequest
41
    {
42
        /** @var AbstractRequest|null $queryAnnotation */
43
        $queryAnnotation = $this->reader->getMethodAnnotation($refMethod, $annotationName);
44
        return $queryAnnotation;
45
    }
46
47
    public function getLoggedAnnotation(ReflectionMethod $refMethod): ?Logged
48
    {
49
        /** @var Logged|null $loggedAnnotation */
50
        $loggedAnnotation = $this->reader->getMethodAnnotation($refMethod, Logged::class);
51
        return $loggedAnnotation;
52
    }
53
54
    public function getRightAnnotation(ReflectionMethod $refMethod): ?Right
55
    {
56
        /** @var Right|null $rightAnnotation */
57
        $rightAnnotation = $this->reader->getMethodAnnotation($refMethod, Right::class);
58
        return $rightAnnotation;
59
    }
60
61
    /**
62
     * @return SourceField[]
63
     */
64
    public function getSourceFields(ReflectionClass $refClass): array
65
    {
66
        /** @var SourceField[] $sourceFields */
67
        $sourceFields = $this->getClassAnnotations($refClass);
68
        $sourceFields = \array_filter($sourceFields, function($annotation): bool {
69
            return $annotation instanceof SourceField;
70
        });
71
        return $sourceFields;
72
    }
73
74
    /**
75
     * Returns a class annotation. Finds in the parents if not found in the main class.
76
     *
77
     * @return object|null
78
     */
79
    private function getClassAnnotation(ReflectionClass $refClass, string $annotationClass)
80
    {
81
        do {
82
            $type = $this->reader->getClassAnnotation($refClass, $annotationClass);
83
            if ($type !== null) {
84
                return $type;
85
            }
86
            $refClass = $refClass->getParentClass();
87
        } while ($refClass);
88
        return null;
89
    }
90
91
    /**
92
     * Returns the class annotations. Finds in the parents too.
93
     *
94
     * @return object[]
95
     */
96
    public function getClassAnnotations(ReflectionClass $refClass): array
97
    {
98
        $annotations = [];
99
        do {
100
            $annotations = array_merge($this->reader->getClassAnnotations($refClass), $annotations);
101
            $refClass = $refClass->getParentClass();
102
        } while ($refClass);
103
        return $annotations;
104
    }
105
106
}
107