Completed
Push — master ( f8b59e...f061a0 )
by Rafael
13:35 queued 07:59
created

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