Completed
Pull Request — master (#6)
by Rafael
09:22
created

resolveFieldMaxConcurrentUsage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 1
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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 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 21
    public function decorateFieldDefinition($field, FieldDefinition $definition, ObjectDefinitionInterface $objectDefinition)
30
    {
31 21
        if (!$field instanceof \ReflectionProperty && !$field instanceof \ReflectionMethod) {
32
            throw new \InvalidArgumentException('Invalid argument, expected reflection of property or method');
33
        }
34
35 21
        if (null !== $name = $this->resolveFieldName($field)) {
36 21
            $definition->setName($name);
37
        }
38
39 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...
40 21
            $definition->setType($this->resolveFieldType($field));
41 21
            $definition->setList($this->resolveFieldIsList($field));
42 21
            $definition->setNonNull($this->resolveFieldNonNull($field));
43 21
            $definition->setNonNullList($this->resolveFieldNonNullList($field));
44
        }
45
46 21
        if (null !== $description = $this->resolveFieldDescription($field)) {
47 21
            $definition->setDescription($description);
48
        }
49
50 21
        if (null !== $deprecationReason = $this->resolveFieldDeprecationReason($field)) {
51
            $definition->setDeprecationReason($deprecationReason);
52
        }
53 21
54
        if (null !== $complexity = $this->resolveFieldComplexity($field)) {
55
            $definition->setComplexity($complexity);
56
        }
57
58
        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
63 21
    /**
64
     * Get field specific annotation matching given objectDefinition
65 21
     *
66 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
67
     * @param string                                $annotationClass
68
     *
69 21
     * @return mixed
70
     */
71
    protected function getFieldAnnotation($prop, string $annotationClass)
72
    {
73
        if ($prop instanceof \ReflectionProperty) {
74
            return $this->reader->getPropertyAnnotation($prop, $annotationClass);
75
        }
76
77 21
        return $this->reader->getMethodAnnotation($prop, $annotationClass);
78
    }
79
80 21
    /**
81 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
82
     *
83
     * @return string|null
84 21
     */
85
    protected function resolveFieldDescription($prop): ?string
86
    {
87
        /** @var Annotation\Field $annotation */
88
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
89
            return $annotation->description;
90
        }
91
92 21
        return null;
93
    }
94
95 21
    /**
96 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
97
     *
98
     * @return string|null
99 21
     */
100
    protected function resolveFieldDeprecationReason($prop): ?string
101
    {
102
        /** @var Annotation\Field $annotation */
103
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
104
            return $annotation->deprecationReason;
105
        }
106
107 21
        return null;
108
    }
109
110 21
    /**
111 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
112 21
     *
113
     * @return string|null
114
     */
115
    protected function resolveFieldComplexity($prop): ?string
116
    {
117
        /** @var Annotation\Field $annotation */
118
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
119
            return $annotation->complexity;
120
        }
121
122
        return null;
123 21
    }
124
125
    /**
126 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
127 21
     *
128 21
     * @return string|null
129
     */
130
    protected function resolveFieldMaxConcurrentUsage($prop): ?string
131
    {
132
        /** @var Annotation\Field $annotation */
133
        if ($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) {
134
            return $annotation->maxConcurrentUsage;
135
        }
136
137
        return 0;
138
    }
139 21
140
    /**
141
     * @param \ReflectionMethod|\ReflectionProperty $prop
142 21
     *
143 21
     * @return bool|null
144 21
     */
145
    protected function resolveFieldNonNull($prop): ?bool
146
    {
147
        /** @var Annotation\Field $annotationField */
148
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
149
        if ($annotationField && $annotationField->type) {
150
            return TypeUtil::isTypeNonNull($annotationField->type);
151
        }
152
153
        return null;
154
    }
155 21
156
    /**
157 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
158
     *
159
     * @return bool|null
160 21
     */
161 21
    protected function resolveFieldNonNullList($prop): ?bool
162 21
    {
163
        /** @var Annotation\Field $annotationField */
164
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
165 21
        if ($annotationField && $annotationField->type) {
166
            return TypeUtil::isTypeNonNullList($annotationField->type);
167
        }
168
169
        return null;
170
    }
171
172
    /**
173 21
     * @param \ReflectionMethod|\ReflectionProperty $prop
174
     *
175
     * @return bool|null
176 21
     */
177 21
    protected function resolveFieldIsList($prop): ?bool
178
    {
179
        /** @var Annotation\Field $annotationField */
180 21
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
181 21
        if ($annotationField && $annotationField->type) {
182
            return TypeUtil::isTypeList($annotationField->type);
183
        }
184 21
185
        return null;
186
    }
187
188
    /**
189
     * @param \ReflectionMethod|\ReflectionProperty $prop
190
     *
191
     * @return string|null
192
     */
193
    protected function resolveFieldType($prop): ?string
194
    {
195
        $type = null;
196
197
        /** @var Annotation\Field $annotationField */
198
        $annotationField = $this->getFieldAnnotation($prop, Annotation\Field::class);
199
        if ($annotationField && $annotationField->type) {
200
            $type = TypeUtil::normalize($annotationField->type);
201
        }
202
203
        return $type;
204
    }
205
206
    /**
207
     * @param \ReflectionMethod|\ReflectionProperty $prop
208
     *
209
     * @return string|null
210
     */
211
    protected function resolveFieldName($prop): ?string
212
    {
213
        /** @var Annotation\Field $annotation */
214
        if (($annotation = $this->getFieldAnnotation($prop, Annotation\Field::class)) && $annotation->name) {
215
            return $annotation->name;
216
        }
217
218
        if ($prop instanceof \ReflectionMethod) {
219
            return lcfirst(preg_replace('/^(get|set)/', null, $prop->name));
220
        }
221
222
        return $prop->name;
223
    }
224
}
225