Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
02:20
created

QueryEdgeCollector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isEnabled() 0 3 1
A getVisitor() 0 21 2
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Visitors;
4
5
use GraphQL\Language\AST\NodeKind;
6
use GraphQL\Language\AST\OperationDefinitionNode;
7
use GraphQL\Language\AST\SelectionSetNode;
8
use GraphQL\Language\AST\VariableDefinition;
9
use GraphQL\Validator\Rules\AbstractQuerySecurity;
10
use GraphQL\Validator\ValidationContext;
11
12
class QueryEdgeCollector extends AbstractQuerySecurity {
13
14
  /**
15
   * @var array
16
   */
17
  private $calculators;
18
19
  /**
20
   * QueryEdgeCollector constructor.
21
   *
22
   * @param array $calculators
23
   */
24
  public function __construct(array $calculators) {
25
    $this->calculators = $calculators;
26
  }
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  protected function isEnabled() {
32
    return !empty($this->calculators);
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function getVisitor(ValidationContext $context) {
39
    $variables = new \ArrayObject();
40
    $structure = new \ArrayObject();
41
42
    return $this->invokeIfNeeded($context, [
43
      NodeKind::SELECTION_SET => function (SelectionSetNode $set) use ($context, &$structure) {
44
        $parent = $context->getParentType();
45
        $structure = $this->collectFieldASTsAndDefs($context, $parent, $set, NULL, $structure);
46
      },
47
      NodeKind::VARIABLE_DEFINITION => function (VariableDefinition $definition) use ($variables) {
48
        $variables->append($definition);
49
      },
50
      NodeKind::OPERATION_DEFINITION => [
51
        'leave' => function (OperationDefinitionNode $definition) use ($context, $variables, $structure) {
52
          foreach ($this->calculators as $visitor) {
53
            $visitor->calculate($definition, $context, $variables, $structure);
54
          }
55
        },
56
      ],
57
    ]);
58
  }
59
}