Completed
Push — master ( a7a918...731c42 )
by Alexandr
03:36
created

ExecutionContext::introduceIntrospectionFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/19/16 9:00 AM
7
*/
8
9
namespace Youshido\GraphQL\Execution\Context;
10
11
12
use Youshido\GraphQL\Execution\Container\ContainerInterface;
13
use Youshido\GraphQL\Execution\Request;
14
use Youshido\GraphQL\Introspection\Field\SchemaField;
15
use Youshido\GraphQL\Introspection\Field\TypeDefinitionField;
16
use Youshido\GraphQL\Schema\AbstractSchema;
17
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
18
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator;
19
20
class ExecutionContext implements ExecutionContextInterface
21
{
22
23
    use ErrorContainerTrait;
24
25
    /** @var AbstractSchema */
26
    private $schema;
27
28
    /** @var Request */
29
    private $request;
30
31
    /** @var ContainerInterface */
32
    private $container;
33
34
    /**
35
     * ExecutionContext constructor.
36
     * @param AbstractSchema $schema
37
     */
38 40
    public function __construct($schema)
39
    {
40 40
        $this->schema = $schema;
41 40
        $this->validateSchema();
42 40
        $this->introduceIntrospectionFields();
43 40
    }
44
45 40
    protected function validateSchema() {
46
        try {
47 40
            (new SchemaValidator())->validate($this->schema);
48 40
        } catch (\Exception $e) {
49 1
            $this->addError($e);
50
        };
51 40
    }
52
53 40
    protected function introduceIntrospectionFields() {
54 40
        $schemaField = new SchemaField();
55 40
        $this->schema->addQueryField($schemaField);
56 40
        $this->schema->addQueryField(new TypeDefinitionField());
57 40
    }
58
59
    /**
60
     * @return AbstractSchema
61
     */
62 28
    public function getSchema()
63
    {
64 28
        return $this->schema;
65
    }
66
67
    /**
68
     * @param AbstractSchema $schema
69
     *
70
     * @return $this
71
     */
72
    public function setSchema(AbstractSchema $schema)
73
    {
74
        $this->schema = $schema;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return Request
81
     */
82 28
    public function getRequest()
83
    {
84 28
        return $this->request;
85
    }
86
87
    /**
88
     * @param Request $request
89
     *
90
     * @return $this
91
     */
92 28
    public function setRequest(Request $request)
93
    {
94 28
        $this->request = $request;
95
96 28
        return $this;
97
    }
98
99
    public function get($id)
100
    {
101
        return $this->container->get($id);
102
    }
103
104
    /**
105
     * @return ContainerInterface
106
     */
107 1
    public function getContainer()
108
    {
109 1
        return $this->container;
110
    }
111
112
    /**
113
     * @param ContainerInterface $container
114
     * @return $this
115
     */
116 30
    public function setContainer(ContainerInterface $container)
117
    {
118 30
        $this->container = $container;
119
120 30
        return $this;
121
    }
122
}
123