Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

GraphQLDocument   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 90
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getDictionary() 0 4 1
A createStandardTypes() 0 13 3
A getStandardTypes() 0 18 1
A getName() 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\Standard;
11
12
use Railt\Io\File;
13
use Railt\SDL\Base\BaseDocument;
14
use Railt\SDL\Contracts\Definitions\Definition;
15
use Railt\SDL\Contracts\Definitions\TypeDefinition;
16
use Railt\SDL\Standard\Directives\Deprecation;
17
use Railt\SDL\Standard\Scalars\AnyType;
18
use Railt\SDL\Standard\Scalars\BooleanType;
19
use Railt\SDL\Standard\Scalars\DateTimeType;
20
use Railt\SDL\Standard\Scalars\FloatType;
21
use Railt\SDL\Standard\Scalars\IDType;
22
use Railt\SDL\Standard\Scalars\IntType;
23
use Railt\SDL\Standard\Scalars\StringType;
24
use Railt\SDL\Reflection\Dictionary;
25
26
/**
27
 * This class contains a Document implementation for
28
 * the standard GraphQL library.
29
 */
30
class GraphQLDocument extends BaseDocument implements StandardType
31
{
32
    /**
33
     * The name of our document constant.
34
     */
35
    private const DOCUMENT_NAME = 'GraphQL Standard Library';
36
37
    /**
38
     * @var array
39
     */
40
    private $additionalTypes;
41
42
    /**
43
     * @var Dictionary
44
     */
45
    private $dictionary;
46
47
    /**
48
     * GraphQLDocument constructor.
49
     * @param Dictionary $dictionary
50
     * @param array|string[] $additionalTypes
51
     */
52 283
    public function __construct(Dictionary $dictionary, array $additionalTypes = [])
53
    {
54 283
        $this->file            = File::fromSources('# Generated');
55 283
        $this->additionalTypes = $additionalTypes;
56
57 283
        $this->createStandardTypes();
58 283
        $this->dictionary = $dictionary;
59 283
    }
60
61
    /**
62
     * @return Dictionary
63
     */
64 12
    public function getDictionary(): Dictionary
65
    {
66 12
        return $this->dictionary;
67
    }
68
69
    /**
70
     * Creation and registration of types.
71
     *
72
     * @return void
73
     */
74 283
    private function createStandardTypes(): void
75
    {
76 283
        foreach ($this->getStandardTypes() as $type) {
77
            /** @var Definition|mixed $instance */
78 283
            $instance = new $type($this);
79
80 283
            if ($instance instanceof TypeDefinition) {
81 283
                $this->types[$instance->getName()] = $instance;
82
            } else {
83 283
                $this->definitions[] = $instance;
84
            }
85
        }
86 283
    }
87
88
    /**
89
     * Returns should return a list of all predefined GraphQL types.
90
     *
91
     * @return array|StandardType[]
92
     */
93 283
    private function getStandardTypes(): array
94
    {
95
        $standard = [
96
            // Scalars
97 283
            BooleanType::class,
98
            FloatType::class,
99
            IDType::class,
100
            IntType::class,
101
            StringType::class,
102
            AnyType::class,
103
            DateTimeType::class,
104
105
            // Directives
106
            Deprecation::class,
107
        ];
108
109 283
        return \array_merge($standard, $this->additionalTypes);
110
    }
111
112
    /**
113
     * @return string
114
     */
115 1
    public function getName(): string
116
    {
117 1
        return self::DOCUMENT_NAME;
118
    }
119
}
120