Test Setup Failed
Push — master ( 1595cb...050b68 )
by Kirill
21:00
created

Context::getDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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;
13
14
use Railt\SDL\Backend\Context\NamedDefinitionContextInterface;
15
use Railt\TypeSystem\Reference\TypeReferenceInterface;
16
use Railt\TypeSystem\Schema;
17
18
/**
19
 * Class Context
20
 */
21
class Context
22
{
23
    /**
24
     * @var Schema
25
     */
26
    private Schema $schema;
27
28
    /**
29
     * @var array|NamedDefinitionContextInterface[]
30
     */
31
    private array $types = [];
32
33
    /**
34
     * @var array|NamedDefinitionContextInterface[]
35
     */
36
    private array $directives = [];
37
38
    /**
39
     * Context constructor.
40
     *
41
     * @param Schema $schema
42
     */
43
    public function __construct(Schema $schema)
44
    {
45
        $this->schema = $schema;
46
    }
47
48
    /**
49
     * @param Schema $schema
50
     * @return void
51
     */
52
    public function setSchema(Schema $schema): void
53
    {
54
        $this->schema = $schema;
55
    }
56
57
    /**
58
     * @return Schema
59
     */
60
    public function getSchema(): Schema
61
    {
62
        return $this->schema;
63
    }
64
65
    /**
66
     * @param NamedDefinitionContextInterface $context
67
     * @return void
68
     */
69
    public function addTypeContext(NamedDefinitionContextInterface $context): void
70
    {
71
        if ($context->getGenericArguments() === []) {
72
            $this->schema->addType($context->resolve());
73
74
            return;
75
        }
76
77
        $this->types[$context->getName()] = $context;
78
    }
79
80
    /**
81
     * @param NamedDefinitionContextInterface $context
82
     * @return void
83
     */
84
    public function addDirectiveContext(NamedDefinitionContextInterface $context): void
85
    {
86
        if ($context->getGenericArguments() === []) {
87
            $this->schema->addDirective($context->resolve());
88
89
            return;
90
        }
91
92
        $this->directives[$context->getName()] = $context;
93
    }
94
95
    /**
96
     * @param string $name
97
     * @return bool
98
     */
99
    public function hasType(string $name): bool
100
    {
101
        return $this->schema->hasType($name)
102
            || isset($this->types[$name]);
103
    }
104
105
    /**
106
     * @param string $type
107
     * @return NamedDefinitionContextInterface|null
108
     */
109
    public function fetch(string $type): ?NamedDefinitionContextInterface
110
    {
111
        return $this->types[$type] ?? null;
112
    }
113
}
114