Passed
Push — master ( 81686d...1595cb )
by Kirill
04:41
created

EnumTypeDefinitionContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 3
A __construct() 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\Type\EnumTypeInterface;
15
use Railt\SDL\Backend\Context;
16
use Railt\SDL\Frontend\Ast\Definition\Type\EnumTypeDefinitionNode;
17
use Railt\TypeSystem\Exception\TypeUniquenessException;
18
use Railt\TypeSystem\Schema;
19
use Railt\TypeSystem\Type\EnumType;
20
21
/**
22
 * @property-read EnumTypeDefinitionNode $ast
23
 * @method EnumTypeDefinitionNode getAst()
24
 */
25
class EnumTypeDefinitionContext extends NamedTypeContext
26
{
27
    /**
28
     * EnumTypeDefinitionContext constructor.
29
     *
30
     * @param Context $context
31
     * @param Schema $schema
32
     * @param EnumTypeDefinitionNode $ast
33
     */
34
    public function __construct(Context $context, Schema $schema, EnumTypeDefinitionNode $ast)
35
    {
36
        parent::__construct($context, $schema, $ast);
37
    }
38
39
    /**
40
     * @param array $variables
41
     * @return EnumTypeInterface
42
     * @throws TypeUniquenessException
43
     * @throws \Throwable
44
     */
45
    public function resolve(array $variables = []): EnumTypeInterface
46
    {
47
        $ast = $this->precompile($this->ast, $variables);
48
49
        $enum = new EnumType($ast->name->value, [
50
            'description' => $this->descriptionOf($ast),
51
        ]);
52
53
        foreach ($ast->values as $value) {
54
            $enum->addValue($this->buildEnumValueDefinition($value));
55
        }
56
57
        foreach ($ast->directives as $directive) {
58
            $this->executeDirective($enum, $directive);
59
        }
60
61
        return $enum;
62
    }
63
}
64