Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

Endpoint::addQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
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\ClassAwareDefinitionInterface;
14
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
15
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
16
use Ynlo\GraphQLBundle\Definition\MutationDefinition;
17
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
18
19
/**
20
 * Class Endpoint
21
 */
22
class Endpoint
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var DefinitionInterface[]
31
     */
32
    protected $types = [];
33
34
    /**
35
     * @var string[]
36
     */
37
    protected $typeMap = [];
38
39
    /**
40
     * @var InterfaceDefinition[]
41
     */
42
    protected $interfaces = [];
43
44
    /**
45
     * @var MutationDefinition[]
46
     */
47
    protected $mutations = [];
48
49
    /**
50
     * @var QueryDefinition[]
51
     */
52
    protected $queries = [];
53
54
    /**
55
     * Endpoint constructor.
56
     *
57
     * @param string $name
58
     */
59 1
    public function __construct(string $name)
60
    {
61 1
        $this->name = $name;
62 1
    }
63
64
    /**
65
     * @return string
66
     */
67 1
    public function getName(): string
68
    {
69 1
        return $this->name;
70
    }
71
72
    /**
73
     * @return DefinitionInterface[]
74
     */
75 1
    public function allTypes(): array
76
    {
77 1
        return $this->types;
78
    }
79
80
    /**
81
     * @param string $type
82
     *
83
     * @return null|string
84
     */
85 15
    public function getClassForType(string $type):?string
86
    {
87 15
        if (isset($this->typeMap[$type])) {
88 15
            return $this->typeMap[$type];
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * @param string $class
96
     *
97
     * @return bool
98
     */
99 22
    public function hasTypeForClass(string $class): bool
100
    {
101 22
        return in_array($class, $this->typeMap);
102
    }
103
104
    /**
105
     * @param string $class
106
     *
107
     * @return string
108
     *
109
     * @throws \UnexpectedValueException if the class does not have any valid type
110
     */
111 22
    public function getTypeForClass(string $class): string
112
    {
113 22
        $typeMap = array_flip($this->typeMap);
114 22
        if (isset($typeMap[$class])) {
115 22
            return $typeMap[$class];
116
        }
117
118
        $error = sprintf('Does not exist any valid type for class "%s"', $class);
119
        throw new \UnexpectedValueException($error);
120
    }
121
122
    /**
123
     * Helper method to avoid in runtime
124
     * recurring all types to get only interfaces
125
     *
126
     * @return InterfaceDefinition[]
127
     */
128 22
    public function allInterfaces(): array
129
    {
130 22
        return $this->interfaces;
131
    }
132
133
    /**
134
     * @return array|MutationDefinition[]
135
     */
136 22
    public function allMutations(): array
137
    {
138 22
        return $this->mutations;
139
    }
140
141
    /**
142
     * @return array|QueryDefinition[]
143
     */
144 22
    public function allQueries(): array
145
    {
146 22
        return $this->queries;
147
    }
148
149
    /**
150
     * @param DefinitionInterface[] $types
151
     */
152 1
    public function setTypes(array $types)
153
    {
154 1
        $this->types = [];
155 1
        $this->interfaces = [];
156 1
        foreach ($types as $type) {
157 1
            $this->addType($type);
158
        }
159 1
    }
160
161
    /**
162
     * @param MutationDefinition[] $mutations
163
     */
164 1
    public function setMutations(array $mutations)
165
    {
166 1
        $this->mutations = [];
167 1
        foreach ($mutations as $mutation) {
168 1
            $this->addMutation($mutation);
169
        }
170 1
    }
171
172
    /**
173
     * @param QueryDefinition[] $queries
174
     */
175 1
    public function setQueries(array $queries)
176
    {
177 1
        $this->queries = [];
178 1
        foreach ($queries as $query) {
179 1
            $this->addQuery($query);
180
        }
181 1
    }
182
183
    /**
184
     * @param string $name
185
     *
186
     * @return bool
187
     */
188 22
    public function hasType($name): bool
189
    {
190 22
        if (class_exists($name) || interface_exists($name)) {
191 22
            if ($this->hasTypeForClass($name)) {
192 7
                $name = $this->getTypeForClass($name);
193
            }
194
        }
195
196 22
        return array_key_exists($name, $this->types);
197
    }
198
199
    /**
200
     * @param string $name
201
     *
202
     * @return bool
203
     */
204 1
    public function hasMutation($name): bool
205
    {
206 1
        return array_key_exists($name, $this->mutations);
207
    }
208
209
    /**
210
     * @param string $name
211
     *
212
     * @return bool
213
     */
214 1
    public function hasQuery($name): bool
215
    {
216 1
        return array_key_exists($name, $this->queries);
217
    }
218
219
    /**
220
     * @param string $name
221
     *
222
     * @return Endpoint
223
     */
224
    public function removeType($name): Endpoint
225
    {
226
        if ($this->hasType($name)) {
227
            $type = $this->getType($name);
228
            if ($type instanceof InterfaceDefinition) {
229
                unset($this->interfaces[$type->getName()]);
230
            }
231
        }
232
233
        unset($this->types[$name]);
234
        if (isset($this->typeMap[$name])) {
235
            unset($this->typeMap[$name]);
236
        }
237
238
        return $this;
239
    }
240
241
    /**
242
     * @param string $name
243
     *
244
     * @return Endpoint
245
     */
246 1
    public function removeQuery($name): Endpoint
247
    {
248 1
        unset($this->queries[$name]);
249
250 1
        return $this;
251
    }
252
253
    /**
254
     * @param string $name
255
     *
256
     * @return Endpoint
257
     */
258
    public function removeMutation($name): Endpoint
259
    {
260
        unset($this->mutations[$name]);
261
262
        return $this;
263
    }
264
265
    /**
266
     * @param string $name
267
     *
268
     * @return DefinitionInterface
269
     *
270
     * @throws \UnexpectedValueException
271
     */
272 22
    public function getType($name)
273
    {
274 22
        if (class_exists($name) || interface_exists($name)) {
275 7
            if ($this->hasTypeForClass($name)) {
276 7
                $name = $this->getTypeForClass($name);
277
            }
278
        }
279
280 22
        if (!$this->hasType($name)) {
281
            throw new \UnexpectedValueException(sprintf('Does not exist a valid definition for "%s" type', $name));
282
        }
283
284 22
        return $this->types[$name];
285
    }
286
287
    /**
288
     * @param string $name
289
     *
290
     * @return MutationDefinition
291
     */
292
    public function getMutation($name): MutationDefinition
293
    {
294
        return $this->mutations[$name];
295
    }
296
297
    /**
298
     * @param string $name
299
     *
300
     * @return QueryDefinition
301
     */
302
    public function getQuery($name): QueryDefinition
303
    {
304
        return $this->queries[$name];
305
    }
306
307
    /**
308
     * @param DefinitionInterface $definition
309
     *
310
     * @return Endpoint
311
     */
312 1
    public function add(DefinitionInterface $definition): Endpoint
313
    {
314 1
        if ($definition instanceof MutationDefinition) {
315
            return $this->addMutation($definition);
316
        }
317
318 1
        if ($definition instanceof QueryDefinition) {
319
            return $this->addQuery($definition);
320
        }
321
322 1
        return $this->addType($definition);
323
    }
324
325
    /**
326
     * @param DefinitionInterface $type
327
     *
328
     * @return Endpoint
329
     */
330 1
    public function addType(DefinitionInterface $type): Endpoint
331
    {
332 1
        if ($this->hasType($type->getName())) {
333
            throw new \RuntimeException(sprintf('Duplicate definition for type with name "%s"', $type->getName()));
334
        }
335 1
        $this->types[$type->getName()] = $type;
336
337 1
        if ($type instanceof InterfaceDefinition) {
338 1
            $this->interfaces[$type->getName()] = $type;
339
        }
340
341 1
        if ($type instanceof ClassAwareDefinitionInterface) {
342 1
            if ($type->getClass()) {
343 1
                $class = $type->getClass();
344
                //all classes are saved without \ at the beginning
345 1
                $class = preg_replace('/^\\\\/', null, $class);
346 1
                $this->typeMap[$type->getName()] = $class;
347
            }
348
        }
349
350 1
        return $this;
351
    }
352
353
    /**
354
     * @param MutationDefinition $mutation
355
     *
356
     * @return Endpoint
357
     */
358 1
    public function addMutation(MutationDefinition $mutation): Endpoint
359
    {
360 1
        if ($this->hasMutation($mutation->getName())) {
361
            throw new \RuntimeException(sprintf('Duplicate definition for query with name "%s"', $mutation->getName()));
362
        }
363 1
        $this->mutations[$mutation->getName()] = $mutation;
364
365 1
        return $this;
366
    }
367
368
    /**
369
     * @param QueryDefinition $query
370
     *
371
     * @return Endpoint
372
     */
373 1
    public function addQuery(QueryDefinition $query): Endpoint
374
    {
375 1
        if ($this->hasQuery($query->getName())) {
376
            throw new \RuntimeException(sprintf('Duplicate definition for query with name "%s"', $query->getName()));
377
        }
378 1
        $this->queries[$query->getName()] = $query;
379
380 1
        return $this;
381
    }
382
}
383