Passed
Push — master ( 43996d...1595cb )
by Kirill
04:07
created

DirectiveContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolve() 0 18 3
A getName() 0 3 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\Context;
13
14
use GraphQL\Contracts\TypeSystem\DirectiveInterface;
15
use Railt\SDL\Backend\Context;
16
use Railt\SDL\Frontend\Ast\Definition\DirectiveDefinitionNode;
17
use Railt\TypeSystem\Directive;
18
use Railt\TypeSystem\Exception\TypeUniquenessException;
19
use Railt\TypeSystem\Schema;
20
21
/**
22
 * @property-read DirectiveDefinitionNode $ast
23
 * @method DirectiveDefinitionNode getAst()
24
 */
25
class DirectiveContext extends DefinitionContext
26
{
27
    /**
28
     * DirectiveContext constructor.
29
     *
30
     * @param Context $context
31
     * @param Schema $schema
32
     * @param DirectiveDefinitionNode $ast
33
     */
34
    public function __construct(Context $context, Schema $schema, DirectiveDefinitionNode $ast)
35
    {
36
        parent::__construct($context, $schema, $ast);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function getName(): string
43
    {
44
        return $this->ast->name->value;
45
    }
46
47
    /**
48
     * @param array $variables
49
     * @return DirectiveInterface
50
     * @throws TypeUniquenessException
51
     * @throws \InvalidArgumentException
52
     * @throws \Throwable
53
     */
54
    public function resolve(array $variables = []): DirectiveInterface
55
    {
56
        $ast = $this->precompile($this->ast, $variables);
57
58
        $directive = new Directive($ast->name->value, [
59
            'description' => $this->descriptionOf($ast),
60
            'repeatable'  => $ast->repeatable !== null,
61
        ]);
62
63
        foreach ($ast->arguments as $argument) {
64
            $directive->addArgument($this->buildArgumentDefinition($argument));
65
        }
66
67
        foreach ($ast->locations as $location) {
68
            $directive->addLocation($location->name->value);
69
        }
70
71
        return $directive;
72
    }
73
}
74