Passed
Push — master ( c39fb6...9e9a0d )
by Rafael
04:42
created

Endpoint::removeType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
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\Registry;
12
13
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
14
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
15
use Ynlo\GraphQLBundle\Definition\MutationDefinition;
16
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
18
19
/**
20
 * Class Endpoint
21
 */
22
class Endpoint
23
{
24
    /**
25
     * @var ObjectDefinitionInterface[]
26
     */
27
    protected $types = [];
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $typeMap = [];
33
34
    /**
35
     * @var InterfaceDefinition[]
36
     */
37
    protected $interfaces = [];
38
39
    /**
40
     * @var MutationDefinition[]
41
     */
42
    protected $mutations = [];
43
44
    /**
45
     * @var QueryDefinition[]
46
     */
47
    protected $queries = [];
48
49
    /**
50
     * @return ObjectDefinitionInterface[]
51
     */
52 1
    public function allTypes(): array
53
    {
54 1
        return $this->types;
55
    }
56
57
    /**
58
     * @param string $type
59
     *
60
     * @return null|string
61
     */
62 12
    public function getClassForType(string $type):?string
63
    {
64 12
        if (isset($this->typeMap[$type])) {
65 12
            return $this->typeMap[$type];
66
        }
67
68 1
        return null;
69
    }
70
71
    /**
72
     * @param string $class
73
     *
74
     * @return bool
75
     */
76 21
    public function hasTypeForClass(string $class): bool
77
    {
78 21
        return in_array($class, $this->typeMap);
79
    }
80
81
    /**
82
     * @param string $class
83
     *
84
     * @return string
85
     *
86
     * @throws \UnexpectedValueException if the class does not have any valid type
87
     */
88 10
    public function getTypeForClass(string $class): string
89
    {
90 10
        $typeMap = array_flip($this->typeMap);
91 10
        if (isset($typeMap[$class])) {
92 10
            return $typeMap[$class];
93
        }
94
95
        $error = sprintf('Does not exist any valid type for class "%s"', $class);
96
        throw new \UnexpectedValueException($error);
97
    }
98
99
    /**
100
     * Helper method to avoid in runtime
101
     * recurring all types to get only interfaces
102
     *
103
     * @return InterfaceDefinition[]
104
     */
105 21
    public function allInterfaces(): array
106
    {
107 21
        return $this->interfaces;
108
    }
109
110
    /**
111
     * @return array|MutationDefinition[]
112
     */
113 21
    public function allMutations(): array
114
    {
115 21
        return $this->mutations;
116
    }
117
118
    /**
119
     * @return array|QueryDefinition[]
120
     */
121 21
    public function allQueries(): array
122
    {
123 21
        return $this->queries;
124
    }
125
126
    /**
127
     * @param ObjectDefinitionInterface[] $types
128
     */
129
    public function setTypes(array $types)
130
    {
131
        $this->types = $types;
132
    }
133
134
    /**
135
     * @param MutationDefinition[] $mutations
136
     */
137 1
    public function setMutations(array $mutations)
138
    {
139 1
        $this->mutations = $mutations;
140 1
    }
141
142
    /**
143
     * @param QueryDefinition[] $queries
144
     */
145 1
    public function setQueries(array $queries)
146
    {
147 1
        $this->queries = $queries;
148 1
    }
149
150
    /**
151
     * @param string $name
152
     *
153
     * @return bool
154
     */
155 21
    public function hasType($name): bool
156
    {
157 21
        if (class_exists($name) || interface_exists($name)) {
158 7
            if ($this->hasTypeForClass($name)) {
159 7
                $name = $this->getTypeForClass($name);
160
            }
161
        }
162
163 21
        return array_key_exists($name, $this->types);
164
    }
165
166
    /**
167
     * @param string $name
168
     *
169
     * @return bool
170
     */
171 1
    public function hasMutation($name): bool
172
    {
173 1
        return array_key_exists($name, $this->mutations);
174
    }
175
176
    /**
177
     * @param string $name
178
     *
179
     * @return bool
180
     */
181 1
    public function hasQuery($name): bool
182
    {
183 1
        return array_key_exists($name, $this->queries);
184
    }
185
186
    /**
187
     * @param string $name
188
     *
189
     * @return Endpoint
190
     */
191
    public function removeType($name): Endpoint
192
    {
193
        unset($this->types[$name]);
194
        if (isset($this->typeMap[$name])) {
195
            unset($this->typeMap[$name]);
196
        }
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param string $name
203
     *
204
     * @return Endpoint
205
     */
206
    public function removeQuery($name): Endpoint
207
    {
208
        unset($this->queries[$name]);
209
210
        return $this;
211
    }
212
213
    /**
214
     * @param string $name
215
     *
216
     * @return Endpoint
217
     */
218
    public function removeMutation($name): Endpoint
219
    {
220
        unset($this->mutations[$name]);
221
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $name
227
     *
228
     * @return ObjectDefinitionInterface
229
     *
230
     * @throws \UnexpectedValueException
231
     */
232 21
    public function getType($name)
233
    {
234 21
        if (class_exists($name) || interface_exists($name)) {
235 10
            if ($this->hasTypeForClass($name)) {
236 10
                $name = $this->getTypeForClass($name);
237
            }
238
        }
239
240 21
        if (!$this->hasType($name)) {
241
            throw new \UnexpectedValueException(sprintf('Does not exist a valid definition for "%s" type', $name));
242
        }
243
244 21
        return $this->types[$name];
245
    }
246
247
    /**
248
     * @param string $name
249
     *
250
     * @return MutationDefinition
251
     */
252
    public function getMutation($name): MutationDefinition
253
    {
254
        return $this->mutations[$name];
255
    }
256
257
    /**
258
     * @param string $name
259
     *
260
     * @return QueryDefinition
261
     */
262
    public function getQuery($name): QueryDefinition
263
    {
264
        return $this->queries[$name];
265
    }
266
267
    /**
268
     * @param DefinitionInterface $definition
269
     *
270
     * @return Endpoint
271
     */
272 1
    public function add(DefinitionInterface $definition): Endpoint
273
    {
274 1
        if ($definition instanceof MutationDefinition) {
275
            return $this->addMutation($definition);
276
        }
277
278 1
        if ($definition instanceof QueryDefinition) {
279
            return $this->addQuery($definition);
280
        }
281
282 1
        return $this->addType($definition);
283
    }
284
285
    /**
286
     * @param ObjectDefinitionInterface $type
287
     *
288
     * @return Endpoint
289
     */
290 1
    public function addType(ObjectDefinitionInterface $type): Endpoint
291
    {
292 1
        if ($this->hasType($type->getName())) {
293
            throw new \RuntimeException(sprintf('Duplicate definition for type with name "%s"', $type->getName()));
294
        }
295 1
        $this->types[$type->getName()] = $type;
296
297 1
        if ($type instanceof InterfaceDefinition) {
298 1
            $this->interfaces[$type->getName()] = $type;
299
        }
300
301 1
        if ($type->getClass()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type->getClass() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
302 1
            $this->typeMap[$type->getName()] = $type->getClass();
303
        }
304
305 1
        return $this;
306
    }
307
308
    /**
309
     * @param MutationDefinition $mutation
310
     *
311
     * @return Endpoint
312
     */
313 1
    public function addMutation(MutationDefinition $mutation): Endpoint
314
    {
315 1
        if ($this->hasMutation($mutation->getName())) {
316
            throw new \RuntimeException(sprintf('Duplicate definition for query with name "%s"', $mutation->getName()));
317
        }
318 1
        $this->mutations[$mutation->getName()] = $mutation;
319
320 1
        return $this;
321
    }
322
323
    /**
324
     * @param QueryDefinition $query
325
     *
326
     * @return Endpoint
327
     */
328 1
    public function addQuery(QueryDefinition $query): Endpoint
329
    {
330 1
        if ($this->hasQuery($query->getName())) {
331
            throw new \RuntimeException(sprintf('Duplicate definition for query with name "%s"', $query->getName()));
332
        }
333 1
        $this->queries[$query->getName()] = $query;
334
335 1
        return $this;
336
    }
337
}
338