UnionTypePluginBase::createInstance()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
rs 9.9
cc 2
nc 1
nop 4
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Unions;
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\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
7
use Drupal\graphql\Plugin\SchemaBuilderInterface;
8
use Drupal\graphql\Plugin\TypePluginInterface;
9
use Drupal\graphql\Plugin\TypePluginManager;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\UnionType;
12
13
abstract class UnionTypePluginBase extends PluginBase implements TypePluginInterface {
14
  use DescribablePluginTrait;
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
20
    return new UnionType([
21
      'name' => $definition['name'],
22
      'description' => $definition['description'],
23
      'types' => function () use ($builder, $definition) {
24
        return array_map(function ($type) use ($builder) {
25
          if (!(($type = $builder->getType($type)) instanceof ObjectType)) {
26
            throw new \LogicException('Union types can only reference object types.');
27
          }
28
29
          return $type;
30
        }, $builder->getSubTypes($definition['name']));
31
      },
32
      'resolveType' => function ($value, $context, $info) use ($builder, $definition) {
33
        return $builder->resolveType($definition['name'], $value, $context, $info);
34
      },
35
    ]);
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function getDefinition() {
42
    $definition = $this->getPluginDefinition();
43
44
    return [
45
      'name' => $definition['name'],
46
      'description' => $this->buildDescription($definition),
47
      'types' => $this->buildTypes($definition),
48
    ];
49
  }
50
51
  /**
52
   * Builds the list of types contained within this union type.
53
   *
54
   * @param array $definition
55
   *   The plugin definion array.
56
   *
57
   * @return array
58
   *   The list of types contained within this union type.
59
   */
60
  protected function buildTypes($definition) {
61
    return $definition['types'] ?: [];
62
  }
63
}
64