Passed
Push — master ( 6bd0cd...6fcec2 )
by David
01:55 queued 10s
created

StaticTypeMapper::mapClassToInputType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
13
/**
14
 * A simple implementation of the TypeMapperInterface that expects mapping to be passed in a setter.
15
 *
16
 * Note: no constructor argument as this results in a loop most of the time.
17
 */
18
final class StaticTypeMapper implements TypeMapperInterface
19
{
20
    /**
21
     * @var array<string,ObjectType>
22
     */
23
    private $types = [];
24
25
    /**
26
     * An array mapping a fully qualified class name to the matching TypeInterface
27
     *
28
     * @param array<string,ObjectType> $types
29
     */
30
    public function setTypes(array $types): void
31
    {
32
        $this->types = $types;
33
    }
34
35
    /**
36
     * @var array<string,InputObjectType>
37
     */
38
    private $inputTypes = [];
39
40
    /**
41
     * An array mapping a fully qualified class name to the matching InputTypeInterface
42
     *
43
     * @param array<string,InputObjectType> $inputTypes
44
     */
45
    public function setInputTypes(array $inputTypes): void
46
    {
47
        $this->inputTypes = $inputTypes;
48
    }
49
50
    /**
51
     * @var array<string,ObjectType|InputObjectType>
52
     */
53
    private $notMappedTypes = [];
54
55
    /**
56
     * An array containing ObjectType or InputObjectType instances that are not mapped by default to any class.
57
     * ObjectType not linked to any type by default will have to be accessed using the outputType attribute of the annotations.
58
     *
59
     * @param array<int,ObjectType|InputObjectType> $types
60
     */
61
    public function setNotMappedTypes(array $types): void
62
    {
63
        $this->notMappedTypes = array_reduce($types, function ($result, Type $type) {
64
            $result[$type->name] = $type;
65
            return $result;
66
        }, []);
67
    }
68
69
    /**
70
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
71
     *
72
     * @param string $className
73
     * @return bool
74
     */
75
    public function canMapClassToType(string $className): bool
76
    {
77
        return isset($this->types[$className]);
78
    }
79
80
    /**
81
     * Maps a PHP fully qualified class name to a GraphQL type.
82
     *
83
     * @param string $className
84
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
85
     * @return ObjectType
86
     * @throws CannotMapTypeException
87
     */
88
    public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType
89
    {
90
        if (isset($this->types[$className])) {
91
            return $this->types[$className];
92
        }
93
        throw CannotMapTypeException::createForType($className);
94
    }
95
96
    /**
97
     * Returns the list of classes that have matching input GraphQL types.
98
     *
99
     * @return string[]
100
     */
101
    public function getSupportedClasses(): array
102
    {
103
        return array_keys($this->types);
104
    }
105
106
    /**
107
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
108
     *
109
     * @param string $className
110
     * @return bool
111
     */
112
    public function canMapClassToInputType(string $className): bool
113
    {
114
        return isset($this->inputTypes[$className]);
115
    }
116
117
    /**
118
     * Maps a PHP fully qualified class name to a GraphQL input type.
119
     *
120
     * @param string $className
121
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
122
     * @return InputObjectType
123
     * @throws CannotMapTypeException
124
     */
125
    public function mapClassToInputType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
126
    {
127
        if (isset($this->inputTypes[$className])) {
128
            return $this->inputTypes[$className];
129
        }
130
        throw CannotMapTypeException::createForInputType($className);
131
    }
132
133
    /**
134
     * Returns a GraphQL type by name (can be either an input or output type)
135
     *
136
     * @param string $typeName The name of the GraphQL type
137
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
138
     * @return Type&(InputType|OutputType)
139
     * @throws CannotMapTypeException
140
     */
141
    public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
142
    {
143
        if (isset($this->notMappedTypes[$typeName])) {
144
            return $this->notMappedTypes[$typeName];
145
        }
146
        foreach ($this->types as $type) {
147
            if ($type->name === $typeName) {
148
                return $type;
149
            }
150
        }
151
        foreach ($this->inputTypes as $inputType) {
152
            if ($inputType->name === $typeName) {
153
                return $inputType;
154
            }
155
        }
156
        throw CannotMapTypeException::createForName($typeName);
157
    }
158
159
    /**
160
     * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type.
161
     *
162
     * @param string $typeName The name of the GraphQL type
163
     * @return bool
164
     */
165
    public function canMapNameToType(string $typeName): bool
166
    {
167
        foreach ($this->types as $type) {
168
            if ($type->name === $typeName) {
169
                return true;
170
            }
171
        }
172
        foreach ($this->inputTypes as $inputType) {
173
            if ($inputType->name === $typeName) {
174
                return true;
175
            }
176
        }
177
        return isset($this->notMappedTypes[$typeName]);
178
    }
179
}
180