Test Setup Failed
Push — master ( 050b68...43996d )
by Kirill
03:02
created

GlobalContext   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 29
dl 0
loc 133
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 3 1
A fetchDirective() 0 7 2
A hasType() 0 4 2
A hasDirective() 0 4 2
A addType() 0 12 2
A setSchema() 0 3 1
A fetchType() 0 7 2
A __construct() 0 4 1
A addDirective() 0 12 2
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Backend\Context;
13
14
use GraphQL\Contracts\TypeSystem\DirectiveInterface;
15
use GraphQL\Contracts\TypeSystem\SchemaInterface;
16
use GraphQL\Contracts\TypeSystem\Type\NamedTypeInterface;
17
use GraphQL\Contracts\TypeSystem\Type\TypeInterface;
18
use Railt\SDL\Backend\NameResolver\NameResolverInterface;
19
use Railt\TypeSystem\Schema;
20
21
/**
22
 * Class GlobalContext
23
 */
24
class GlobalContext implements ContextInterface
25
{
26
    /**
27
     * @var array|LocalContextInterface[]
28
     */
29
    public array $types;
30
31
    /**
32
     * @var array|LocalContextInterface[]
33
     */
34
    public array $directives;
35
36
    /**
37
     * @var Schema
38
     */
39
    private Schema $schema;
40
41
    /**
42
     * @var NameResolverInterface
43
     */
44
    private NameResolverInterface $resolver;
45
46
    /**
47
     * GlobalContext constructor.
48
     *
49
     * @param NameResolverInterface $resolver
50
     * @param Schema $schema
51
     */
52
    public function __construct(NameResolverInterface $resolver, Schema $schema)
53
    {
54
        $this->schema = $schema;
55
        $this->resolver = $resolver;
56
    }
57
58
    /**
59
     * @param Schema $schema
60
     * @return void
61
     */
62
    public function setSchema(Schema $schema): void
63
    {
64
        $this->schema = $schema;
65
    }
66
67
    /**
68
     * @return SchemaInterface
69
     */
70
    public function getSchema(): SchemaInterface
71
    {
72
        return $this->schema;
73
    }
74
75
    /**
76
     * @param LocalContextInterface $context
77
     * @return void
78
     */
79
    public function addType(LocalContextInterface $context): void
80
    {
81
        if ($context->getGenericArguments() === []) {
82
            /** @var TypeInterface $type */
83
            $type = $context->build([]);
84
85
            $this->schema->addType($type);
86
87
            return;
88
        }
89
90
        $this->types[$context->getName()] = $context;
91
    }
92
93
    /**
94
     * @param string $type
95
     * @return bool
96
     */
97
    public function hasType(string $type): bool
98
    {
99
        return isset($this->types[$type])
100
            || $this->schema->hasType($type);
101
    }
102
103
    /**
104
     * @param string $type
105
     * @param array|string[] $args
106
     * @return TypeInterface
107
     */
108
    public function fetchType(string $type, array $args = []): TypeInterface
109
    {
110
        if ($this->schema->hasType($type)) {
111
            return $this->schema->getType($type);
112
        }
113
114
        return $this->types[$type]->build($args);
115
    }
116
117
    /**
118
     * @param LocalContextInterface $context
119
     * @return void
120
     */
121
    public function addDirective(LocalContextInterface $context): void
122
    {
123
        if ($context->getGenericArguments() === []) {
124
            /** @var DirectiveInterface $directive */
125
            $directive = $context->build([]);
126
127
            $this->schema->addDirective($directive);
128
129
            return;
130
        }
131
132
        $this->directives[$context->getName()] = $context;
133
    }
134
135
    /**
136
     * @param string $type
137
     * @return bool
138
     */
139
    public function hasDirective(string $type): bool
140
    {
141
        return isset($this->directives[$type])
142
            || $this->schema->getDirective($type);
143
    }
144
145
    /**
146
     * @param string $type
147
     * @param array $args
148
     * @return DirectiveInterface|NamedTypeInterface
149
     */
150
    public function fetchDirective(string $type, array $args = []): DirectiveInterface
151
    {
152
        if ($directive = $this->schema->getDirective($type)) {
153
            return $directive;
154
        }
155
156
        return $this->directives[$type]->build($args);
157
    }
158
}
159