Completed
Pull Request — master (#60)
by David
01:54
created

StaticTypeMapper::mapNameToType()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
cc 5
nc 7
nop 2
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
5
use function array_keys;
6
use GraphQL\Type\Definition\InputType;
7
use GraphQL\Type\Definition\ObjectType;
8
use GraphQL\Type\Definition\OutputType;
9
use GraphQL\Type\Definition\Type;
10
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...
11
12
/**
13
 * A simple implementation of the TypeMapperInterface that expects mapping to be passed in a setter.
14
 *
15
 * Note: no constructor argument as this results in a loop most of the time.
16
 */
17
final class StaticTypeMapper implements TypeMapperInterface
18
{
19
    /**
20
     * @var array<string,ObjectType>
21
     */
22
    private $types;
23
24
    /**
25
     * An array mapping a fully qualified class name to the matching TypeInterface
26
     *
27
     * @param array<string,ObjectType> $types
28
     */
29
    public function setTypes(array $types): void
30
    {
31
        $this->types = $types;
32
    }
33
34
    /**
35
     * @var array<string,InputType>
36
     */
37
    private $inputTypes;
38
39
    /**
40
     * An array mapping a fully qualified class name to the matching InputTypeInterface
41
     *
42
     * @param array<string,InputType> $inputTypes
43
     */
44
    public function setInputTypes(array $inputTypes): void
45
    {
46
        $this->inputTypes = $inputTypes;
47
    }
48
49
    /**
50
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
51
     *
52
     * @param string $className
53
     * @return bool
54
     */
55
    public function canMapClassToType(string $className): bool
56
    {
57
        return isset($this->types[$className]);
58
    }
59
60
    /**
61
     * Maps a PHP fully qualified class name to a GraphQL type.
62
     *
63
     * @param string $className
64
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
65
     * @return ObjectType
66
     * @throws CannotMapTypeException
67
     */
68
    public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType
69
    {
70
        if (isset($this->types[$className])) {
71
            return $this->types[$className];
72
        }
73
        throw CannotMapTypeException::createForType($className);
74
    }
75
76
    /**
77
     * Returns the list of classes that have matching input GraphQL types.
78
     *
79
     * @return string[]
80
     */
81
    public function getSupportedClasses(): array
82
    {
83
        return array_keys($this->types);
84
    }
85
86
    /**
87
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
88
     *
89
     * @param string $className
90
     * @return bool
91
     */
92
    public function canMapClassToInputType(string $className): bool
93
    {
94
        return isset($this->inputTypes[$className]);
95
    }
96
97
    /**
98
     * Maps a PHP fully qualified class name to a GraphQL input type.
99
     *
100
     * @param string $className
101
     * @return InputType
102
     * @throws CannotMapTypeException
103
     */
104
    public function mapClassToInputType(string $className): InputType
105
    {
106
        if (isset($this->inputTypes[$className])) {
107
            return $this->inputTypes[$className];
108
        }
109
        throw CannotMapTypeException::createForInputType($className);
110
    }
111
112
    /**
113
     * Returns a GraphQL type by name (can be either an input or output type)
114
     *
115
     * @param string $typeName The name of the GraphQL type
116
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
117
     * @return Type&(InputType|OutputType)
118
     * @throws CannotMapTypeException
119
     */
120
    public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
121
    {
122
        foreach ($this->types as $type) {
123
            if ($type->name === $typeName) {
124
                return $type;
125
            }
126
        }
127
        foreach ($this->inputTypes as $inputType) {
128
            if ($inputType->name === $typeName) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Type\Definition\InputType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
129
                return $inputType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $inputType returns the type GraphQL\Type\Definition\InputType which is incompatible with the type-hinted return GraphQL\Type\Definition\Type.
Loading history...
130
            }
131
        }
132
        throw CannotMapTypeException::createForName($typeName);
133
    }
134
135
    /**
136
     * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type.
137
     *
138
     * @param string $typeName The name of the GraphQL type
139
     * @return bool
140
     */
141
    public function canMapNameToType(string $typeName): bool
142
    {
143
        foreach ($this->types as $type) {
144
            if ($type->name === $typeName) {
145
                return true;
146
            }
147
        }
148
        foreach ($this->inputTypes as $inputType) {
149
            if ($inputType->name === $typeName) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Type\Definition\InputType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
150
                return true;
151
            }
152
        }
153
        return false;
154
    }
155
}
156