TypeResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 68
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D resolveType() 0 29 9
A addType() 0 10 2
A getType() 0 6 2
1
<?php
2
3
namespace Arthem\GraphQLMapper\Schema;
4
5
use GraphQL\Type\Definition\Type;
6
7
class TypeResolver
8
{
9
    /**
10
     * @var Type[]
11
     */
12
    private $types = [];
13
14
    /**
15
     * @param string $name
16
     * @return Type
17
     */
18
    public function resolveType($name)
19
    {
20
        if (preg_match('#^(.+)\!$#', $name, $regs)) {
21
            return Type::nonNull($this->resolveType($regs[1]));
22
        }
23
24
        if (preg_match('#^\[(.+)\]$#', $name, $regs)) {
25
            return Type::listOf($this->resolveType($regs[1]));
26
        }
27
28
        switch ($name) {
29
            case Type::INT:
30
                return Type::int();
31
            case Type::STRING:
32
                return Type::string();
33
            case Type::BOOLEAN:
34
                return Type::boolean();
35
            case Type::FLOAT:
36
                return Type::float();
37
            case Type::ID:
38
                return Type::id();
39
            default:
40
                if (!isset($this->types[$name])) {
41
                    throw new \InvalidArgumentException(sprintf('Type "%s" is not defined', $name));
42
                }
43
44
                return $this->types[$name];
45
        }
46
    }
47
48
    /**
49
     * @param string $name
50
     * @param Type   $type
51
     * @return $this
52
     */
53
    public function addType($name, Type $type)
54
    {
55
        if (isset($this->types[$name])) {
56
            throw new \RuntimeException(sprintf('Type "%s" is already defined', $name));
57
        }
58
59
        $this->types[$name] = $type;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $name
66
     * @return Type|null
67
     */
68
    public function getType($name)
69
    {
70
        if (isset($this->types[$name])) {
71
            return $this->types[$name];
72
        }
73
    }
74
}
75