Completed
Pull Request — master (#63)
by David
02:48
created

AnnotationReader::getFactoryAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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