Test Failed
Push — master ( 08b170...e6bc2a )
by Kirill
02:02
created

SchemaBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Builder\Definition;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Reflection\Contracts\Definition;
14
use Railt\Reflection\Definition\SchemaDefinition;
15
16
/**
17
 * Class SchemaBuilder
18
 */
19
class SchemaBuilder extends TypeDefinitionBuilder
20
{
21
    /**
22
     * @return Definition
23
     */
24
    public function build(): Definition
25
    {
26
        $schema = $this->bind(new SchemaDefinition($this->document, $this->findName()));
27
28
        foreach ($this->ast as $child) {
29
            $this->async(function () use ($child, $schema) {
30
                $this->buildField($child, $schema);
31
            });
32
        }
33
34
        return $schema;
35
    }
36
37
    /**
38
     * @param RuleInterface $rule
39
     * @param SchemaDefinition $definition
40
     * @throws \Railt\Io\Exception\ExternalFileException
41
     */
42
    private function buildField(RuleInterface $rule, SchemaDefinition $definition): void
43
    {
44
        switch ($rule[0]->getValue()) {
45
            case 'query':
46
                $definition->withQuery($this->loadFieldType($rule[1]));
47
                break;
48
49
            case 'mutation':
50
                $definition->withMutation($this->loadFieldType($rule[1]));
51
                break;
52
53
            case 'subscription':
54
                $definition->withSubscription($this->loadFieldType($rule[1]));
55
                break;
56
        }
57
    }
58
59
    /**
60
     * @param RuleInterface $rule
61
     * @return string|null
62
     */
63
    private function loadFieldType(RuleInterface $rule)
64
    {
65
        return $rule->getValue();
66
    }
67
}
68