Passed
Pull Request — master (#22)
by David
03:09
created

CompositeTypeMapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 19
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canMapClassToType() 0 8 3
A mapClassToInputType() 0 8 3
A canMapClassToInputType() 0 8 3
A mapClassToType() 0 8 3
A __construct() 0 4 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
    public function __construct(array $typeMappers)
18
    {
19
20
        $this->typeMappers = $typeMappers;
21
    }
22
23
    /**
24
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
25
     *
26
     * @param string $className
27
     * @return bool
28
     */
29
    public function canMapClassToType(string $className): bool
30
    {
31
        foreach ($this->typeMappers as $typeMapper) {
32
            if ($typeMapper->canMapClassToType($className)) {
33
                return true;
34
            }
35
        }
36
        return false;
37
    }
38
39
    /**
40
     * Maps a PHP fully qualified class name to a GraphQL type.
41
     *
42
     * @param string $className
43
     * @return TypeInterface
44
     * @throws CannotMapTypeException
45
     */
46
    public function mapClassToType(string $className): TypeInterface
47
    {
48
        foreach ($this->typeMappers as $typeMapper) {
49
            if ($typeMapper->canMapClassToType($className)) {
50
                return $typeMapper->mapClassToType($className);
51
            }
52
        }
53
        throw CannotMapTypeException::createForType($className);
54
    }
55
56
    /**
57
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
58
     *
59
     * @param string $className
60
     * @return bool
61
     */
62
    public function canMapClassToInputType(string $className): bool
63
    {
64
        foreach ($this->typeMappers as $typeMapper) {
65
            if ($typeMapper->canMapClassToInputType($className)) {
66
                return true;
67
            }
68
        }
69
        return false;
70
    }
71
72
    /**
73
     * Maps a PHP fully qualified class name to a GraphQL input type.
74
     *
75
     * @param string $className
76
     * @return InputTypeInterface
77
     * @throws CannotMapTypeException
78
     */
79
    public function mapClassToInputType(string $className): InputTypeInterface
80
    {
81
        foreach ($this->typeMappers as $typeMapper) {
82
            if ($typeMapper->canMapClassToInputType($className)) {
83
                return $typeMapper->mapClassToInputType($className);
84
            }
85
        }
86
        throw CannotMapTypeException::createForInputType($className);
87
    }
88
}
89