Completed
Pull Request — master (#43)
by Quang
01:56
created

ExecutionContextBuilder::buildContext()   C

Complexity

Conditions 10
Paths 7

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 7
nop 7
dl 0
loc 51
rs 6
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Digia\GraphQL\Execution;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\NodeKindEnum;
8
use Digia\GraphQL\Type\Schema;
9
10
/**
11
 * Class ExecutionContextBuilder
12
 * @package Digia\GraphQL\Execution
13
 */
14
class ExecutionContextBuilder
15
{
16
    /**
17
     * @param Schema        $schema
18
     * @param DocumentNode  $documentNode
19
     * @param               $rootValue
20
     * @param               $contextValue
21
     * @param               $rawVariableValues
22
     * @param null          $operationName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $operationName is correct as it would always require null to be passed?
Loading history...
23
     * @param callable|null $fieldResolver
24
     * @throws GraphQLError
25
     * @return ExecutionContext
26
     */
27
    public function buildContext(
28
        Schema $schema,
29
        DocumentNode $documentNode,
30
        $rootValue,
31
        $contextValue,
32
        $rawVariableValues,
33
        $operationName = null,
34
        callable $fieldResolver = null
35
    ): ExecutionContext {
36
        //@TODO validate $rawVariableValues?
37
38
        $errors    = [];
39
        $fragments = [];
40
        $operation = null;
41
42
        foreach ($documentNode->getDefinitions() as $definition) {
43
            switch ($definition->getKind()) {
44
                case NodeKindEnum::OPERATION_DEFINITION:
45
                    if (!$operationName && $operation) {
46
                        throw new GraphQLError(
47
                            'Must provide operation name if query contains multiple operations.'
48
                        );
49
                    }
50
51
                    if (!$operationName || (!empty($definition->getName()) && $definition->getName()->getValue() === $operationName)) {
52
                        $operation = $definition;
53
                    }
54
                    break;
55
                case NodeKindEnum::FRAGMENT_DEFINITION:
56
                case NodeKindEnum::FRAGMENT_SPREAD:
57
                    $fragments[$definition->getName()->getValue()] = $definition;
58
                    break;
59
                default:
60
                    throw new GraphQLError(
61
                        "GraphQL cannot execute a request containing a {$definition->getKind()}."
62
                    );
63
            }
64
        }
65
66
        $executionContext = new ExecutionContext(
67
            $schema,
68
            $fragments,
69
            $rootValue,
70
            $contextValue,
71
            $rawVariableValues,
72
            $fieldResolver,
73
            $operation,
74
            $errors
75
        );
76
77
        return $executionContext;
78
    }
79
}