Completed
Push — master ( 186982...2975e7 )
by Rafael
05:12
created

resolveFieldRoles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 Doctrine\Common\Annotations\Reader;
14
use Ynlo\GraphQLBundle\Annotation;
15
use Ynlo\GraphQLBundle\Definition\FieldDefinition;
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
    /**
25
     * @var Reader
26
     */
27
    protected $reader;
28
29
    /**
30
     * GraphQLFieldDefinitionDecorator constructor.
31
     *
32
     * @param Reader $reader
33
     */
34 26
    public function __construct(Reader $reader)
35
    {
36 26
        $this->reader = $reader;
37 26
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 21
    public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition)
43
    {
44 21
        if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) {
45
            throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method');
46
        }
47
48 21
        if (null !== $name = $this->resolveFieldName($field)) {
49 21
            $definition->setName($name);
50
        }
51
52 21
        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...
53 17
            $definition->setType($this->resolveFieldType($field));
54 17
            $definition->setList($this->resolveFieldIsList($field));
55 17
            $definition->setNonNull($this->resolveFieldNonNull($field));
56 17
            $definition->setNonNullList($this->resolveFieldNonNullList($field));
57
        }
58
59 21
        if (null !== $description = $this->resolveFieldDescription($field)) {
60 8
            $definition->setDescription($description);
61
        }
62
63 21
        if ($metas = $this->resolveFieldMetas($field)) {
64
            $definition->setMetas(array_merge($metas, $definition->getMetas()));
65
        }
66
67 21
        if (null !== $deprecationReason = $this->resolveFieldDeprecationReason($field)) {
68 8
            $definition->setDeprecationReason($deprecationReason);
69
        }
70
71 21
        if (null !== $complexity = $this->resolveFieldComplexity($field)) {
72
            $definition->setComplexity($complexity);
73
        }
74
75 21
        if ($maxConcurrentUsage = $this->resolveFieldMaxConcurrentUsage($field)) {
76
            $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

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