Completed
Pull Request — master (#7)
by Yonel Ceruto
10:45 queued 04:14
created

resolveFieldDeprecationReason()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation\FieldDecorator;
12
13
use Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationReaderAwareTrait;
16
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
17
use Ynlo\GraphQLBundle\Util\TypeUtil;
18
19
/**
20
 * Decorate a field definition using common GraphQL annotations
21
 */
22
class GraphQLFieldDefinitionDecorator implements FieldDefinitionDecoratorInterface
23
{
24
    use AnnotationReaderAwareTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 22
    public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition)
30
    {
31 22
        if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) {
32
            throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method');
33
        }
34
35 22
        if (null !== $name = $this->resolveFieldName($field)) {
36 22
            $definition->setName($name);
37
        }
38
39 22
        if (null !== $type = $this->resolveFieldType($field)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
40 22
            $definition->setType($this->resolveFieldType($field));
41 22
            $definition->setList($this->resolveFieldIsList($field));
42 22
            $definition->setNonNull($this->resolveFieldNonNull($field));
43 22
            $definition->setNonNullList($this->resolveFieldNonNullList($field));
44
        }
45
46 22
        if (null !== $description = $this->resolveFieldDescription($field)) {
47 22
            $definition->setDescription($description);
48
        }
49
50 22
        if (null !== $deprecationReason = $this->resolveFieldDeprecationReason($field)) {
51
            $definition->setDeprecationReason($deprecationReason);
52
        }
53
54 22
        if (null !== $complexity = $this->resolveFieldComplexity($field)) {
55
            $definition->setComplexity($complexity);
56
        }
57
58 22
        if ($maxConcurrentUsage = $this->resolveFieldMaxConcurrentUsage($field)) {
59
            $definition->setMaxConcurrentUsage($maxConcurrentUsage);
0 ignored issues
show
Bug introduced by
$maxConcurrentUsage of type string is incompatible with the type integer expected by parameter $maxConcurrentUsage of Ynlo\GraphQLBundle\Defin...setMaxConcurrentUsage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            $definition->setMaxConcurrentUsage(/** @scrutinizer ignore-type */ $maxConcurrentUsage);
Loading history...
60
        }
61
62 22
        if ($roles = $this->resolveFieldRoles($field)) {
63
            $definition->setRoles($roles);
64
        }
65 22
    }
66
67
    /**
68
     * Get field specific annotation matching given objectDefinition
69
     *
70
     * @param \ReflectionMethod|\ReflectionProperty $prop
71
     * @param string                                $annotationClass
72
     *
73
     * @return mixed
74
     */
75 22
    protected function getFieldAnnotation($prop, string $annotationClass)
76
    {
77 22
        if ($prop instanceof \ReflectionProperty) {
78 22
            return $this->reader->getPropertyAnnotation($prop, $annotationClass);
79
        }
80
81 22
        return $this->reader->getMethodAnnotation($prop, $annotationClass);
82
    }
83
84
    /**
85
     * @param \ReflectionMethod|\ReflectionProperty $prop
86
     *
87
     * @return string|null
88
     */
89 22
    protected function resolveFieldDescription($prop): ?string
90
    {
91
        /** @var Annotation\Field $annotation */
92 22
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
93 22
            return $annotation->description;
94
        }
95
96 22
        return null;
97
    }
98
99
    /**
100
     * @param \ReflectionMethod|\ReflectionProperty $prop
101
     *
102
     * @return string|null
103
     */
104 22
    protected function resolveFieldDeprecationReason($prop): ?string
105
    {
106
        /** @var Annotation\Field $annotation */
107 22
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
108 22
            return $annotation->deprecationReason;
109
        }
110
111 22
        return null;
112
    }
113
114
    /**
115
     * @param \ReflectionMethod|\ReflectionProperty $prop
116
     *
117
     * @return string|null
118
     */
119 22
    protected function resolveFieldComplexity($prop): ?string
120
    {
121
        /** @var Annotation\Field $annotation */
122 22
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
123 22
            return $annotation->complexity;
124
        }
125
126 22
        return null;
127
    }
128
129
    /**
130
     * @param \ReflectionMethod|\ReflectionProperty $prop
131
     *
132
     * @return string|null
133
     */
134 22
    protected function resolveFieldMaxConcurrentUsage($prop): ?string
135
    {
136
        /** @var Annotation\Field $annotation */
137 22
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
138 22
            return $annotation->maxConcurrentUsage;
139
        }
140
141 22
        return 0;
142
    }
143
144
    /**
145
     * @param \ReflectionMethod|\ReflectionProperty $prop
146
     *
147
     * @return array
148
     */
149 22
    private function resolveFieldRoles($prop): array
150
    {
151
        /** @var Annotation\Field $annotation */
152 22
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
153 22
            return (array) $annotation->roles;
154
        }
155
156 22
        return [];
157
    }
158
159
    /**
160
     * @param \ReflectionMethod|\ReflectionProperty $prop
161
     *
162
     * @return bool|null
163
     */
164 22
    protected function resolveFieldNonNull($prop): ?bool
165
    {
166
        /** @var Annotation\Field $annotationField */
167 22
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
168 22
        if ($annotationField && $annotationField->type) {
0 ignored issues
show
introduced by
The condition $annotationField && $annotationField->type can never be true.
Loading history...
169 22
            return TypeUtil::isTypeNonNull($annotationField->type);
170
        }
171
172
        return null;
173
    }
174
175
    /**
176
     * @param \ReflectionMethod|\ReflectionProperty $prop
177
     *
178
     * @return bool|null
179
     */
180 22
    protected function resolveFieldNonNullList($prop): ?bool
181
    {
182
        /** @var Annotation\Field $annotationField */
183 22
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
184 22
        if ($annotationField && $annotationField->type) {
0 ignored issues
show
introduced by
The condition $annotationField && $annotationField->type can never be true.
Loading history...
185 22
            return TypeUtil::isTypeNonNullList($annotationField->type);
186
        }
187
188
        return null;
189
    }
190
191
    /**
192
     * @param \ReflectionMethod|\ReflectionProperty $prop
193
     *
194
     * @return bool|null
195
     */
196 22
    protected function resolveFieldIsList($prop): ?bool
197
    {
198
        /** @var Annotation\Field $annotationField */
199 22
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
200 22
        if ($annotationField && $annotationField->type) {
0 ignored issues
show
introduced by
The condition $annotationField && $annotationField->type can never be true.
Loading history...
201 22
            return TypeUtil::isTypeList($annotationField->type);
202
        }
203
204
        return null;
205
    }
206
207
    /**
208
     * @param \ReflectionMethod|\ReflectionProperty $prop
209
     *
210
     * @return string|null
211
     */
212 22
    protected function resolveFieldType($prop): ?string
213
    {
214 22
        $type = null;
215
216
        /** @var Annotation\Field $annotationField */
217 22
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
218 22
        if ($annotationField && $annotationField->type) {
0 ignored issues
show
introduced by
The condition $annotationField && $annotationField->type can never be true.
Loading history...
219 22
            $type = TypeUtil::normalize($annotationField->type);
220
        }
221
222 22
        return $type;
223
    }
224
225
    /**
226
     * @param \ReflectionMethod|\ReflectionProperty $prop
227
     *
228
     * @return string|null
229
     */
230 22
    protected function resolveFieldName($prop): ?string
231
    {
232
        /** @var Annotation\Field $annotation */
233 22
        if (($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) && $annotation->name) {
234 22
            return $annotation->name;
235
        }
236
237 22
        if ($prop instanceof \ReflectionMethod) {
238 22
            return lcfirst(preg_replace('/^(get|set)/', null, $prop->name));
239
        }
240
241 22
        return $prop->name;
242
    }
243
}
244