InterfacePluginBase::getDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Interfaces;
4
5
use Drupal\Component\Plugin\PluginBase;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Plugin\PluginBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Cache\Cache;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Cache\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\SchemaBuilderInterface;
10
use Drupal\graphql\Plugin\TypePluginInterface;
11
use Drupal\graphql\Plugin\TypePluginManager;
12
use GraphQL\Type\Definition\InterfaceType;
13
14
abstract class InterfacePluginBase extends PluginBase implements TypePluginInterface {
15
  use CacheablePluginTrait;
16
  use DescribablePluginTrait;
17
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
22
    return new InterfaceType([
23
      'name' => $definition['name'],
24
      'description' => $definition['description'],
25
      'contexts' => function () use ($builder, $definition) {
26
        $types = $builder->getSubTypes($definition['name']);
27
28
        return array_reduce($types, function ($carry, $current) use ($builder) {
29
          $type = $builder->getType($current);
30
          if (!empty($type->config['contexts'])) {
31
            $contexts = $type->config['contexts'];
32
            $contexts = is_callable($contexts) ? $contexts() : $contexts;
33
            return Cache::mergeContexts($carry, $contexts);
34
          }
35
36
          return $carry;
37
        }, $definition['contexts']);
38
      },
39
      'fields' => function () use ($builder, $definition) {
40
        $fields = $builder->getFields($definition['name']);
41
42
        if (!empty($definition['interfaces'])) {
43
          $inherited = array_map(function ($name) use ($builder) {
44
            return $builder->getFields($name);
45
          }, $definition['interfaces']);
46
47
          $inherited = call_user_func_array('array_merge', $inherited);
48
          return array_merge($inherited, $fields);
49
        }
50
51
        return $fields;
52
      },
53
      'resolveType' => function ($value, $context, $info) use ($builder, $definition) {
54
        return $builder->resolveType($definition['name'], $value, $context, $info);
55
      },
56
    ]);
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getDefinition() {
63
    $definition = $this->getPluginDefinition();
64
65
    return [
66
      'name' => $definition['name'],
67
      'description' => $this->buildDescription($definition),
68
      'interfaces' => $this->buildInterfaces($definition),
69
      'contexts' => $this->buildCacheContexts($definition),
70
    ];
71
  }
72
73
  /**
74
   * Builds the list of interfaces inherited by this interface.
75
   *
76
   * @param array $definition
77
   *   The plugin definition array.
78
   *
79
   * @return array
80
   *   The list of interfaces that this interface inherits from.
81
   */
82
  protected function buildInterfaces($definition) {
83
    return $definition['interfaces'] ?: [];
84
  }
85
86
}
87