StaticTypeMapper   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 39
dl 0
loc 218
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A mapClassToType() 0 11 3
A extendTypeForClass() 0 3 1
A extendTypeForName() 0 3 1
A canExtendTypeForClass() 0 3 1
A canMapNameToType() 0 13 5
A mapNameToType() 0 16 6
A setTypes() 0 3 1
A canMapClassToInputType() 0 3 1
A mapClassToInputType() 0 6 2
A canExtendTypeForName() 0 3 1
A setInputTypes() 0 3 1
A getSupportedClasses() 0 3 1
A canMapClassToType() 0 3 1
A setNotMappedTypes() 0 6 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
5
use function array_keys;
6
use GraphQL\Type\Definition\InputObjectType;
7
use GraphQL\Type\Definition\InputType;
8
use GraphQL\Type\Definition\ObjectType;
9
use GraphQL\Type\Definition\OutputType;
10
use GraphQL\Type\Definition\Type;
11
use TheCodingMachine\GraphQL\Controllers\Mappers\Interfaces\InterfacesResolverInterface;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\GraphQL...rfacesResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType;
13
14
/**
15
 * A simple implementation of the TypeMapperInterface that expects mapping to be passed in a setter.
16
 *
17
 * Note: no constructor argument as this results in a loop most of the time.
18
 */
19
final class StaticTypeMapper implements TypeMapperInterface
20
{
21
    /**
22
     * @var array<string,MutableObjectType>
23
     */
24
    private $types = [];
25
26
    /**
27
     * An array mapping a fully qualified class name to the matching TypeInterface
28
     *
29
     * @param array<string,MutableObjectType> $types
30
     */
31
    public function setTypes(array $types): void
32
    {
33
        $this->types = $types;
34
    }
35
36
    /**
37
     * @var array<string,InputObjectType>
38
     */
39
    private $inputTypes = [];
40
41
    /**
42
     * An array mapping a fully qualified class name to the matching InputTypeInterface
43
     *
44
     * @param array<string,InputObjectType> $inputTypes
45
     */
46
    public function setInputTypes(array $inputTypes): void
47
    {
48
        $this->inputTypes = $inputTypes;
49
    }
50
51
    /**
52
     * @var array<string,MutableObjectType|InputObjectType>
53
     */
54
    private $notMappedTypes = [];
55
56
    /**
57
     * An array containing ObjectType or InputObjectType instances that are not mapped by default to any class.
58
     * ObjectType not linked to any type by default will have to be accessed using the outputType attribute of the annotations.
59
     *
60
     * @param array<int,Type> $types
61
     */
62
    public function setNotMappedTypes(array $types): void
63
    {
64
        $this->notMappedTypes = array_reduce($types, function ($result, Type $type) {
65
            $result[$type->name] = $type;
66
            return $result;
67
        }, []);
68
    }
69
70
    /**
71
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
72
     *
73
     * @param string $className
74
     * @return bool
75
     */
76
    public function canMapClassToType(string $className): bool
77
    {
78
        return isset($this->types[$className]);
79
    }
80
81
    /**
82
     * Maps a PHP fully qualified class name to a GraphQL type.
83
     *
84
     * @param string $className
85
     * @param OutputType|null $subType
86
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
87
     * @return MutableObjectType
88
     * @throws CannotMapTypeExceptionInterface
89
     */
90
    public function mapClassToType(string $className, ?OutputType $subType, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType
91
    {
92
        // TODO: add support for $subType
93
        if ($subType !== null) {
94
            throw CannotMapTypeException::createForType($subType);
0 ignored issues
show
Bug introduced by
$subType of type GraphQL\Type\Definition\OutputType is incompatible with the type string expected by parameter $className of TheCodingMachine\GraphQL...eption::createForType(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            throw CannotMapTypeException::createForType(/** @scrutinizer ignore-type */ $subType);
Loading history...
95
        }
96
97
        if (isset($this->types[$className])) {
98
            return $this->types[$className];
99
        }
100
        throw CannotMapTypeException::createForType($className);
101
    }
102
103
    /**
104
     * Returns the list of classes that have matching input GraphQL types.
105
     *
106
     * @return string[]
107
     */
108
    public function getSupportedClasses(): array
109
    {
110
        return array_keys($this->types);
111
    }
112
113
    /**
114
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
115
     *
116
     * @param string $className
117
     * @return bool
118
     */
119
    public function canMapClassToInputType(string $className): bool
120
    {
121
        return isset($this->inputTypes[$className]);
122
    }
123
124
    /**
125
     * Maps a PHP fully qualified class name to a GraphQL input type.
126
     *
127
     * @param string $className
128
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
129
     * @return InputObjectType
130
     * @throws CannotMapTypeExceptionInterface
131
     */
132
    public function mapClassToInputType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
133
    {
134
        if (isset($this->inputTypes[$className])) {
135
            return $this->inputTypes[$className];
136
        }
137
        throw CannotMapTypeException::createForInputType($className);
138
    }
139
140
    /**
141
     * Returns a GraphQL type by name (can be either an input or output type)
142
     *
143
     * @param string $typeName The name of the GraphQL type
144
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
145
     * @return Type&(InputType|OutputType)
146
     * @throws CannotMapTypeExceptionInterface
147
     */
148
    public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
149
    {
150
        if (isset($this->notMappedTypes[$typeName])) {
151
            return $this->notMappedTypes[$typeName];
152
        }
153
        foreach ($this->types as $type) {
154
            if ($type->name === $typeName) {
155
                return $type;
156
            }
157
        }
158
        foreach ($this->inputTypes as $inputType) {
159
            if ($inputType->name === $typeName) {
160
                return $inputType;
161
            }
162
        }
163
        throw CannotMapTypeException::createForName($typeName);
164
    }
165
166
    /**
167
     * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type.
168
     *
169
     * @param string $typeName The name of the GraphQL type
170
     * @return bool
171
     */
172
    public function canMapNameToType(string $typeName): bool
173
    {
174
        foreach ($this->types as $type) {
175
            if ($type->name === $typeName) {
176
                return true;
177
            }
178
        }
179
        foreach ($this->inputTypes as $inputType) {
180
            if ($inputType->name === $typeName) {
181
                return true;
182
            }
183
        }
184
        return isset($this->notMappedTypes[$typeName]);
185
    }
186
187
    /**
188
     * Returns true if this type mapper can extend an existing type for the $className FQCN
189
     *
190
     * @param string $className
191
     * @param MutableObjectType $type
192
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
193
     * @return bool
194
     */
195
    public function canExtendTypeForClass(string $className, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): bool
196
    {
197
        return false;
198
    }
199
200
    /**
201
     * Extends the existing GraphQL type that is mapped to $className.
202
     *
203
     * @param string $className
204
     * @param MutableObjectType $type
205
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
206
     * @throws CannotMapTypeExceptionInterface
207
     */
208
    public function extendTypeForClass(string $className, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): void
209
    {
210
        throw CannotMapTypeException::createForExtendType($className, $type);
211
    }
212
213
    /**
214
     * Returns true if this type mapper can extend an existing type for the $typeName GraphQL type
215
     *
216
     * @param string $typeName
217
     * @param MutableObjectType $type
218
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
219
     * @return bool
220
     */
221
    public function canExtendTypeForName(string $typeName, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): bool
222
    {
223
        return false;
224
    }
225
226
    /**
227
     * Extends the existing GraphQL type that is mapped to the $typeName GraphQL type.
228
     *
229
     * @param string $typeName
230
     * @param MutableObjectType $type
231
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
232
     * @throws CannotMapTypeExceptionInterface
233
     */
234
    public function extendTypeForName(string $typeName, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): void
235
    {
236
        throw CannotMapTypeException::createForExtendName($typeName, $type);
237
    }
238
}
239