Completed
Push — master ( 5db45c...f208af )
by Arthur
02:39
created

YamlDriver::createType()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 12
rs 9.4286
cc 3
eloc 8
nc 1
nop 2
1
<?php
2
namespace Arthem\GraphQLMapper\Mapping\Driver;
3
4
use Arthem\GraphQLMapper\Mapping\AbstractType;
5
use Arthem\GraphQLMapper\Mapping\Field;
6
use Arthem\GraphQLMapper\Mapping\FieldContainer;
7
use Arthem\GraphQLMapper\Mapping\InterfaceType;
8
use Arthem\GraphQLMapper\Mapping\Query;
9
use Arthem\GraphQLMapper\Mapping\SchemaContainer;
10
use Arthem\GraphQLMapper\Mapping\Type;
11
use Symfony\Component\Yaml\Yaml;
12
13
class YamlDriver extends FileDriver
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function load(SchemaContainer $schema)
19
    {
20
        $paths = $this->getPaths();
21
22
        foreach ($paths as $path) {
23
            $this->loadFile($path, $schema);
24
        }
25
    }
26
27
    /**
28
     * @param string          $path
29
     * @param SchemaContainer $schemaContainer
30
     */
31
    private function loadFile($path, SchemaContainer $schemaContainer)
32
    {
33
        $config = Yaml::parse($this->getFileContent($path));
34
35
        foreach ($config as $type => $mapping) {
36
            switch ($type) {
37
                case 'query':
38
                    $querySchema = $schemaContainer->getQuerySchema();
39
                    if (null === $querySchema) {
40
                        $querySchema = new Query();
41
                        $schemaContainer->setQuerySchema($querySchema);
42
                    }
43
44
                    $this->populateFieldContainer($querySchema, $mapping);
45
                    break;
46
                case 'mutation':
47
                    $mutationSchema = $schemaContainer->getMutationSchema();
48
                    if (null === $mutationSchema) {
49
                        $mutationSchema = new Query();
50
                        $schemaContainer->setMutationSchema($mutationSchema);
51
                    }
52
53
                    $this->populateFieldContainer($mutationSchema, $mapping);
54
                    break;
55
                case 'types':
56
                    foreach ($mapping as $name => $typeMapping) {
57
                        $type = $this->createType($name, $typeMapping);
58
                        $schemaContainer->addType($type);
59
                    }
60
                    break;
61
                case 'interfaces':
62
                    foreach ($mapping as $name => $interfaceMapping) {
63
                        $interface = $this->createInterface($name, $interfaceMapping);
64
                        $schemaContainer->addInterface($interface);
65
                    }
66
                    break;
67
                default:
68
                    throw new \UnexpectedValueException(sprintf('Unsupported key "%s"'));
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param string $name
75
     * @param array  $mapping
76
     * @return Type
77
     */
78
    private function createType($name, array $mapping)
79
    {
80
        $type = new Type();
81
        $type
82
            ->setName($name)
83
            ->setExtends(isset($mapping['extends']) ? $mapping['extends'] : null)
84
            ->setResolveConfig(isset($mapping['resolve']) ? $mapping['resolve'] : []);
85
86
        $this->populateFieldContainer($type, $mapping);
87
88
        return $type;
89
    }
90
91
    /**
92
     * @param AbstractType $type
93
     * @param array        $mapping
94
     */
95
    private function populateType(AbstractType $type, array $mapping)
96
    {
97
        if (isset($mapping['description'])) {
98
            $type->setDescription($mapping['description']);
99
        }
100
    }
101
102
    /**
103
     * @param FieldContainer $type
104
     * @param array          $mapping
105
     */
106
    private function populateFieldContainer(FieldContainer $type, array $mapping)
107
    {
108
        $this->populateType($type, $mapping);
109
110
        if (!isset($mapping['fields'])) {
111
            return;
112
        }
113
114
        $fields = [];
115
        foreach ($mapping['fields'] as $name => $fieldMapping) {
116
            $fields[] = $this->createField($name, $fieldMapping);
117
        }
118
        $type->setFields($fields);
119
    }
120
121
    /**
122
     * @param string $name
123
     * @param array  $mapping
124
     * @return InterfaceType
125
     */
126
    private function createInterface($name, array $mapping)
127
    {
128
        $interface = new InterfaceType();
129
        $interface->setName($name);
130
        $this->populateFieldContainer($interface, $mapping);
131
132
        return $interface;
133
    }
134
135
    /**
136
     * @param string $name
137
     * @param array  $mapping
138
     * @return Field
139
     */
140
    private function createField($name, array $mapping)
141
    {
142
        $field = new Field();
143
        $field
144
            ->setName($name)
145
            ->setType(isset($mapping['type']) ? $mapping['type'] : null)
146
            ->setField(isset($mapping['field']) ? $mapping['field'] : null)
147
            ->setResolveConfig(isset($mapping['resolve']) ? $mapping['resolve'] : []);
148
149
        $this->populateType($field, $mapping);
150
151
        if (isset($mapping['args'])) {
152
            $args = [];
153
            foreach ($mapping['args'] as $argName => $argMapping) {
154
                $args[] = $this->createField($argName, $argMapping);
155
            }
156
            $field->setArguments($args);
157
        }
158
159
        return $field;
160
    }
161
}
162