Completed
Push — master ( 8e6ca0...1e6eb5 )
by David
17s queued 11s
created

RecursiveTypeMapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 14
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canMapClassToInputType() 0 3 1
A mapClassToInputType() 0 3 1
A __construct() 0 3 1
A canMapClassToType() 0 8 3
A mapClassToType() 0 8 3
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Mappers;
5
6
7
use function get_parent_class;
8
use GraphQL\Type\Definition\InputType;
9
use GraphQL\Type\Definition\OutputType;
10
use GraphQL\Type\Definition\Type;
11
12
/**
13
 * This class wraps a TypeMapperInterface into a RecursiveTypeMapperInterface.
14
 * While the wrapped class does only tests one given class, the recursive type mapper
15
 * tests the class and all its parents.
16
 */
17
class RecursiveTypeMapper implements RecursiveTypeMapperInterface
18
{
19
    /**
20
     * @var TypeMapperInterface
21
     */
22
    private $typeMapper;
23
24
    public function __construct(TypeMapperInterface $typeMapper)
25
    {
26
        $this->typeMapper = $typeMapper;
27
    }
28
29
    /**
30
     * Returns true if this type mapper can map the $className FQCN to a GraphQL type.
31
     *
32
     * @param string $className The class name to look for (this function looks into parent classes if the class does not match a type).
33
     * @return bool
34
     */
35
    public function canMapClassToType(string $className): bool
36
    {
37
        do {
38
            if ($this->typeMapper->canMapClassToType($className)) {
39
                return true;
40
            }
41
        } while ($className = get_parent_class($className));
42
        return false;
43
    }
44
45
    /**
46
     * Maps a PHP fully qualified class name to a GraphQL type.
47
     *
48
     * @param string $className The class name to look for (this function looks into parent classes if the class does not match a type).
49
     * @return OutputType&Type
50
     * @throws CannotMapTypeException
51
     */
52
    public function mapClassToType(string $className): OutputType
53
    {
54
        do {
55
            if ($this->typeMapper->canMapClassToType($className)) {
56
                return $this->typeMapper->mapClassToType($className);
57
            }
58
        } while ($className = get_parent_class($className));
59
        throw CannotMapTypeException::createForType($className);
60
    }
61
62
    /**
63
     * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
64
     *
65
     * @param string $className
66
     * @return bool
67
     */
68
    public function canMapClassToInputType(string $className): bool
69
    {
70
        return $this->typeMapper->canMapClassToInputType($className);
71
    }
72
73
    /**
74
     * Maps a PHP fully qualified class name to a GraphQL input type.
75
     *
76
     * @param string $className
77
     * @return InputType&Type
78
     * @throws CannotMapTypeException
79
     */
80
    public function mapClassToInputType(string $className): InputType
81
    {
82
        return $this->typeMapper->mapClassToInputType($className);
83
    }
84
}
85