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

UnionTypePluginBase::buildConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Unions;
4
5
use Drupal\Core\Cache\CacheableDependencyInterface;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\graphql\Plugin\GraphQL\SchemaBuilder;
9
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
10
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait;
11
use Drupal\graphql\Plugin\GraphQL\Traits\PluginTrait;
12
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Youshido\GraphQL\Config\Object\UnionTypeConfig;
15
use Youshido\GraphQL\Type\Union\AbstractUnionType;
16
17
/**
18
 * Base class for GraphQL union type plugins.
19
 */
20
abstract class UnionTypePluginBase extends AbstractUnionType implements TypeSystemPluginInterface {
21
  use PluginTrait;
22
  use CacheablePluginTrait;
23
  use NamedPluginTrait;
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function __construct(array $configuration, $pluginId, $pluginDefinition) {
29
    $this->constructPlugin($configuration, $pluginId, $pluginDefinition);
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35 View Code Duplication
  public function buildConfig(SchemaBuilder $schemaManager) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
    $name = $this->buildName();
37
38
    $this->config = new UnionTypeConfig([
39
      'name' => $name,
40
      'description' => $this->buildDescription(),
41
      'types' => $this->buildTypes($schemaManager, $name),
42
    ]);
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function build($config) {
49
    // May be overridden, but not required any more.
50
  }
51
52
  /**
53
   * Builds the list of types that are contained within this union type.
54
   *
55
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaBuilder $schemaManager
56
   *   The schema manager.
57
   * @param $name
58
   *   The name of this plugin.
59
   *
60
   * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[]
61
   *   An array of types to add to this union type.
62
   */
63
  protected function buildTypes(SchemaBuilder $schemaManager, $name) {
64
    /** @var \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[] $types */
65
    $types = $schemaManager->find(function ($type) use ($name) {
66
      return in_array($name, $type['unions']);
67
    }, [
68
      GRAPHQL_TYPE_PLUGIN,
69
    ]);
70
71
    $types = array_merge($this->getPluginDefinition()['parents'], $types);
72
    return array_unique($types);
73
  }
74
75
  /**
76
   * Default implementation of "resolveType".
77
   *
78
   * Checks all implementing types and returns the matching type with the
79
   * highest weight.
80
   *
81
   * @param mixed $object
82
   *   The current response tree value.
83
   *
84
   * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase
85
   *   The type object.
86
   */
87
  public function resolveType($object) {
88
    /** @var \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase $type */
89
    foreach ($this->getTypes() as $type) {
90
      if ($type->applies($object)) {
91
        return $type;
92
      }
93
    }
94
95
    return NULL;
96
  }
97
98
  /**
99
   * Default implementation of "getTypes".
100
   *
101
   * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase[]
102
   *   The types contained within this union type.
103
   */
104
  public function getTypes() {
105
    return $this->getConfig()->get('types', []);
106
  }
107
108
}
109