Completed
Push — master ( 708c0e...18f94d )
by Alexandr
28:28 queued 14:38
created

ExecutionContext::getField()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 4
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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\Field\Field;
15
use Youshido\GraphQL\Introspection\Field\SchemaField;
16
use Youshido\GraphQL\Introspection\Field\TypeDefinitionField;
17
use Youshido\GraphQL\Schema\AbstractSchema;
18
use Youshido\GraphQL\Type\Object\AbstractObjectType;
19
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
20
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator;
21
22
class ExecutionContext implements ExecutionContextInterface
23
{
24
25
    use ErrorContainerTrait;
26
27
    /** @var AbstractSchema */
28
    private $schema;
29
30
    /** @var Request */
31
    private $request;
32
33
    /** @var ContainerInterface */
34
    private $container;
35
36
    /** @var array */
37
    private $typeFieldLookupTable;
38
39 74
    /**
40
     * ExecutionContext constructor.
41 74
     *
42 74
     * @param AbstractSchema $schema
43
     */
44 74
    public function __construct(AbstractSchema $schema)
45 74
    {
46
        $this->schema = $schema;
47 74
        $this->validateSchema();
48
49
        $this->introduceIntrospectionFields();
50 74
51 1
        $this->typeFieldLookupTable = [];
52 1
    }
53
54 74
    /**
55
     * @param AbstractObjectType $type
56 74
     * @param string             $fieldName
57
     * 
58 74
     * @return Field
59 74
     */
60 74
    public function getField(AbstractObjectType $type, $fieldName)
61 74
    {
62
        $typeName = $type->getName();
63
64
        if (!array_key_exists($typeName, $this->typeFieldLookupTable)) {
65
            $this->typeFieldLookupTable[$typeName] = [];
66 68
        }
67
68 68
        if (!array_key_exists($fieldName, $this->typeFieldLookupTable[$typeName])) {
69
            $this->typeFieldLookupTable[$typeName][$fieldName] = $type->getField($fieldName);
70
        }
71
72
        return $this->typeFieldLookupTable[$typeName][$fieldName];
73
    }
74
75
    protected function validateSchema()
76
    {
77
        try {
78
            (new SchemaValidator())->validate($this->schema);
79
        } catch (\Exception $e) {
80
            $this->addError($e);
81
        };
82
    }
83
84
    protected function introduceIntrospectionFields()
85
    {
86 68
        $schemaField = new SchemaField();
87
        $this->schema->addQueryField($schemaField);
88 68
        $this->schema->addQueryField(new TypeDefinitionField());
89
    }
90
91
    /**
92
     * @return AbstractSchema
93
     */
94
    public function getSchema()
95
    {
96 68
        return $this->schema;
97
    }
98 68
99
    /**
100 68
     * @param AbstractSchema $schema
101
     *
102
     * @return $this
103
     */
104
    public function setSchema(AbstractSchema $schema)
105
    {
106
        $this->schema = $schema;
107
108
        return $this;
109
    }
110
111 1
    /**
112
     * @return Request
113 1
     */
114
    public function getRequest()
115
    {
116
        return $this->request;
117
    }
118
119
    /**
120
     * @param Request $request
121 71
     *
122
     * @return $this
123 71
     */
124
    public function setRequest(Request $request)
125 71
    {
126
        $this->request = $request;
127
128
        return $this;
129
    }
130
131
    public function get($id)
132
    {
133
        return $this->container->get($id);
134
    }
135
136
    /**
137
     * @return ContainerInterface
138
     */
139
    public function getContainer()
140
    {
141
        return $this->container;
142
    }
143
144
    /**
145
     * @param ContainerInterface $container
146
     *
147
     * @return $this
148
     */
149
    public function setContainer(ContainerInterface $container)
150
    {
151
        $this->container = $container;
152
153
        return $this;
154
    }
155
}
156