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

SchemaBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 12 2
A buildField() 0 16 4
A loadFieldType() 0 4 1
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