Completed
Push — master ( adbe6b...3f4eee )
by David
10s
created

CompositeTypeMapper::canMapClassToInputType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
5
6
7
use Youshido\GraphQL\Type\InputTypeInterface;
8
use Youshido\GraphQL\Type\TypeInterface;
9
10
class CompositeTypeMapper implements TypeMapperInterface
11
{
12
    /**
13
     * @var TypeMapperInterface[]
14
     */
15
    private $typeMappers;
16
17
    /**
18
     * @param TypeMapperInterface[] $typeMappers
19
     */
20
    public function setTypeMappers(array $typeMappers): void
21
    {
22
        // TODO: move the setter in the constructor in v3
23
        // We can do this if we get rid of the Registry god object which is easier if we don't have to inject it in the
24
        // AbsractAnnotatedObjectType class (this class will disappear in v3)
25
        $this->typeMappers = $typeMappers;
26
    }
27
28
    /**
29
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
30
     *
31
     * @param string $className
32
     * @return bool
33
     */
34
    public function canMapClassToType(string $className): bool
35
    {
36
        foreach ($this->typeMappers as $typeMapper) {
37
            if ($typeMapper->canMapClassToType($className)) {
38
                return true;
39
            }
40
        }
41
        return false;
42
    }
43
44
    /**
45
     * Maps a PHP fully qualified class name to a GraphQL type.
46
     *
47
     * @param string $className
48
     * @return TypeInterface
49
     * @throws CannotMapTypeException
50
     */
51
    public function mapClassToType(string $className): TypeInterface
52
    {
53
        foreach ($this->typeMappers as $typeMapper) {
54
            if ($typeMapper->canMapClassToType($className)) {
55
                return $typeMapper->mapClassToType($className);
56
            }
57
        }
58
        throw CannotMapTypeException::createForType($className);
59
    }
60
61
    /**
62
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
63
     *
64
     * @param string $className
65
     * @return bool
66
     */
67
    public function canMapClassToInputType(string $className): bool
68
    {
69
        foreach ($this->typeMappers as $typeMapper) {
70
            if ($typeMapper->canMapClassToInputType($className)) {
71
                return true;
72
            }
73
        }
74
        return false;
75
    }
76
77
    /**
78
     * Maps a PHP fully qualified class name to a GraphQL input type.
79
     *
80
     * @param string $className
81
     * @return InputTypeInterface
82
     * @throws CannotMapTypeException
83
     */
84
    public function mapClassToInputType(string $className): InputTypeInterface
85
    {
86
        foreach ($this->typeMappers as $typeMapper) {
87
            if ($typeMapper->canMapClassToInputType($className)) {
88
                return $typeMapper->mapClassToInputType($className);
89
            }
90
        }
91
        throw CannotMapTypeException::createForInputType($className);
92
    }
93
}
94