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

ObjectTypeDefinitionContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

2 Methods

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