Completed
Pull Request — master (#18)
by Quang
03:15
created

ExecutionStrategy::executeFields()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 4
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Execution;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Language\AST\Node\ArgumentNode;
7
use Digia\GraphQL\Language\AST\Node\FieldNode;
8
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode;
9
use Digia\GraphQL\Language\AST\Node\InputValueDefinitionNode;
10
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode;
11
use Digia\GraphQL\Language\AST\Node\SelectionSetNode;
12
use Digia\GraphQL\Language\AST\NodeKindEnum;
13
use Digia\GraphQL\Type\Definition\ObjectType;
14
15
/**
16
 * Class AbstractStrategy
17
 * @package Digia\GraphQL\Execution\Strategies
18
 */
19
abstract class ExecutionStrategy
20
{
21
    /**
22
     * @var ExecutionContext
23
     */
24
    protected $context;
25
26
    /**
27
     * @var OperationDefinitionNode
28
     */
29
    protected $operation;
30
31
    /**
32
     * @var mixed
33
     */
34
    protected $rootValue;
35
36
    /**
37
     * AbstractStrategy constructor.
38
     * @param ExecutionContext $context
39
     *
40
     * @param OperationDefinitionNode $operation
41
     */
42
    public function __construct(
43
        ExecutionContext $context,
44
        OperationDefinitionNode $operation,
45
        $rootValue)
46
    {
47
        $this->context   = $context;
48
        $this->operation = $operation;
49
        $this->rootValue = $rootValue;
50
    }
51
52
    /**
53
     * @return array|null
54
     */
55
    abstract function execute(): ?array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
57
    /**
58
     * @param ObjectType $runtimeType
59
     * @param SelectionSetNode $selectionSet
60
     * @param $fields
61
     * @param $visitedFragmentNames
62
     * @return \ArrayObject
63
     */
64
    protected function collectFields(
65
        ObjectType $runtimeType,
66
        SelectionSetNode $selectionSet,
67
        $fields,
68
        $visitedFragmentNames
69
    ) {
70
        foreach ($selectionSet->getSelections() as $selection) {
71
            /** @var FieldNode $selection */
72
            switch ($selection->getKind()) {
73
                case NodeKindEnum::FIELD:
74
                    $name = $selection->getNameValue();
75
                    if (!isset($fields[$name])) {
76
                        $fields[$name] = new \ArrayObject();
77
                    }
78
                    $fields[$name][] = $selection;
79
                    break;
80
                case NodeKindEnum::INLINE_FRAGMENT:
81
                    //TODO check if should include this node
82
                    $this->collectFields(
83
                        $runtimeType,
84
                        $selection->getSelectionSet(),
85
                        $fields,
86
                        $visitedFragmentNames
87
                    );
88
                    break;
89
                case NodeKindEnum::FRAGMENT_SPREAD:
90
                    //TODO check if should include this node
91
                    if (!empty($visitedFragmentNames[$selection->getNameValue()])) {
92
                        continue;
93
                    }
94
                    $visitedFragmentNames[$selection->getNameValue()] = true;
95
                    /** @var FragmentDefinitionNode $fragment */
96
                    $fragment = $this->context->getFragments()[$selection->getNameValue()];
97
                    $this->collectFields(
98
                        $runtimeType,
99
                        $fragment->getSelectionSet(),
100
                        $fields,
101
                        $visitedFragmentNames
102
                    );
103
                    break;
104
            }
105
        }
106
107
        return $fields;
108
    }
109
110
    /**
111
     * Implements the "Evaluating selection sets" section of the spec
112
     * for "read" mode.
113
     * @param ObjectType $parentType
114
     * @param $source
115
     * @param $path
116
     * @param $fields
117
     *
118
     * @return array
119
     *
120
     * @throws GraphQLError|\Exception
121
     */
122
    protected function executeFields(
123
        ObjectType $parentType,
124
        $source,
125
        $path,
126
        $fields): array
127
    {
128
        $finalResults = [];
129
130
        foreach ($fields as $fieldName => $fieldNodes) {
131
            $fieldPath   = $path;
132
            $fieldPath[] = $fieldName;
133
            if (!$this->isDefinedField($parentType, $fieldName)) {
134
                continue;
135
            }
136
137
            $result = $this->resolveField($parentType,
138
                $source,
139
                $fieldNodes,
140
                $fieldPath
141
            );
142
143
            $finalResults[$fieldName] = $result;
144
        }
145
146
        return $finalResults;
147
    }
148
149
    /**
150
     * @param ObjectType $parentType
151
     * @param string $fieldName
152
     * @return bool
153
     * @throws \Exception
154
     */
155
    protected function isDefinedField(ObjectType $parentType, string $fieldName)
156
    {
157
       return isset($parentType->getFields()[$fieldName]);
158
    }
159
160
    /**
161
     * @param ObjectType $parentType
162
     * @param $source
163
     * @param $fieldNodes
164
     * @param $path
165
     *
166
     * @return mixed
167
     *
168
     * @throws GraphQLError|\Exception
169
     */
170
    protected function resolveField(
171
        ObjectType $parentType,
172
        $source,
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

172
        /** @scrutinizer ignore-unused */ $source,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
        $fieldNodes,
174
        $path)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
        /** @scrutinizer ignore-unused */ $path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        /** @var FieldNode $fieldNode */
177
        $fieldNode = $fieldNodes[0];
178
179
        $field = $parentType->getFields()[$fieldNode->getNameValue()];
180
181
        $inputValues = $fieldNode->getArguments() ?? [];
182
183
        $args = [];
184
185
        foreach ($inputValues as $value) {
186
            if ($value instanceof ArgumentNode) {
187
                $args[] = $value->getValue()->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Digia\GraphQL\Language\A...ract\ValueNodeInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Digia\GraphQL\Language\AST\Node\VariableNode or Digia\GraphQL\Language\AST\Node\ListValueNode or Digia\GraphQL\Language\AST\Node\ObjectValueNode or Digia\GraphQL\Language\AST\Node\NullValueNode. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
                $args[] = $value->getValue()->/** @scrutinizer ignore-call */ getValue();
Loading history...
188
            } elseif ($value instanceof InputValueDefinitionNode) {
189
                $args[] = $value->getDefaultValue()->getValue();
190
            }
191
        }
192
193
        $result = $field->resolve(...$args);
194
195
        //TODO Resolve sub fields
196
197
        return $result;
198
    }
199
200
    private function completeValue(
0 ignored issues
show
Unused Code introduced by
The method completeValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
201
        $returnType,
0 ignored issues
show
Unused Code introduced by
The parameter $returnType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

201
        /** @scrutinizer ignore-unused */ $returnType,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
        $fieldNodes,
0 ignored issues
show
Unused Code introduced by
The parameter $fieldNodes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
        /** @scrutinizer ignore-unused */ $fieldNodes,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
        $info,
0 ignored issues
show
Unused Code introduced by
The parameter $info is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

203
        /** @scrutinizer ignore-unused */ $info,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
204
        $path,
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

204
        /** @scrutinizer ignore-unused */ $path,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
205
        &$result)
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

205
        /** @scrutinizer ignore-unused */ &$result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
    {
207
208
209
    }
210
}
211