Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

DefinitionManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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