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

CacheContextsCollector::collectCacheContexts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Visitors;
4
5
use Drupal\Core\Cache\Cache;
6
use GraphQL\Language\AST\FieldNode;
7
use GraphQL\Language\AST\NodeKind;
8
use GraphQL\Type\Definition\LeafType;
9
use GraphQL\Utils\TypeInfo;
10
11
class CacheContextsCollector {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function getVisitor(TypeInfo $info, array &$contexts) {
17
    return [
18
      NodeKind::FIELD => [
19
        'leave' => function (FieldNode $field) use ($info, &$contexts) {
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

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

Loading history...
20
          $definition = $info->getFieldDef();
21 View Code Duplication
          if (!empty($definition->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($definition->config['contexts']));
23
          }
24
25
          $parent = $info->getParentType();
26 View Code Duplication
          if (!empty($parent->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($parent->config['contexts']));
28
          }
29
30
          $type = $info->getType();
31
          // Collect cache metadata from leaf types.
32 View Code Duplication
          if ($type instanceof LeafType && !empty($type->config['contexts'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            $contexts = Cache::mergeContexts($contexts, $this->collectCacheContexts($type->config['contexts']));
34
          }
35
        },
36
      ],
37
    ];
38
  }
39
40
  /**
41
   * Collects the cache contexts from a type or field config.
42
   *
43
   * @param array|callable $contexts
44
   *   The cache contexts array or a callable to return cache contexts.
45
   *
46
   * @return array
47
   *   The collected cache contexts.
48
   */
49
  protected function collectCacheContexts($contexts) {
50
    if (is_callable($contexts)) {
51
      return $contexts();
52
    }
53
54
    return $contexts;
55
  }
56
}