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

SchemaPluginBase   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 12.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 16
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 4 1
B getSchema() 16 31 3
A extractDefinitions() 0 5 1
A getMutations() 0 3 1
A getRootFields() 0 3 1
A getFields() 0 4 2
A getTypes() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder;
9
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface;
10
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
11
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Schema;
14
use GraphQL\Type\SchemaConfig;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, ContainerFactoryPluginInterface {
18
  use DependencySerializationTrait;
19
20
  /**
21
   * The schema builder object.
22
   *
23
   * @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface
24
   */
25
  protected $schemaBuilder;
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
31
    return new static(
32
      $configuration,
33
      $plugin_id,
34
      $plugin_definition,
35
      $container->get('graphql.plugin_manager_aggregator')
36
    );
37
  }
38
39
  /**
40
   * SchemaPluginBase constructor.
41
   *
42
   * @param array $configuration
43
   *   The plugin configuration array.
44
   * @param string $pluginId
45
   *   The plugin id.
46
   * @param array $pluginDefinition
47
   *   The plugin definition array.
48
   * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers
49
   *   Type system plugin manager aggregator service.
50
   */
51
  public function __construct($configuration, $pluginId, $pluginDefinition, TypeSystemPluginManagerAggregator $pluginManagers) {
52
    parent::__construct($configuration, $pluginId, $pluginDefinition);
53
    $this->schemaBuilder = new PluggableSchemaBuilder($pluginManagers);
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function getSchema() {
60
    $config = new SchemaConfig();
61
62 View Code Duplication
    if ($mutations = $this->getMutations()) {
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...
63
      $config->setMutation(new ObjectType([
64
        'name' => 'MutationRoot',
65
        'fields' => function () use ($mutations) {
66
          return $this->schemaBuilder->resolveFields($mutations);
67
        }
68
      ]));
69
    }
70
71 View Code Duplication
    if ($query = $this->getRootFields()) {
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...
72
      $config->setQuery(new ObjectType([
73
        'name' => 'QueryRoot',
74
        'fields' => function () use ($query) {
75
          return $this->schemaBuilder->resolveFields($query);
76
        }
77
      ]));
78
    }
79
80
    $config->setTypes(function () {
81
      return $this->getTypes();
82
    });
83
84
    $config->setTypeLoader(function ($name) {
85
      return $this->schemaBuilder->getTypeByName($name);
86
    });
87
88
    return new Schema($config);
89
  }
90
91
  /**
92
   * Extract type or field definitions from plugins.
93
   *
94
   * @param array $plugins
95
   *   The list of plugins to extract the type or field definitions from.
96
   *
97
   * @return array
98
   *   The list of extracted type or field definitions.
99
   */
100
  protected function extractDefinitions(array $plugins) {
101
    return array_filter(array_map(function (TypeSystemPluginInterface $plugin) {
102
      return $plugin->getDefinition($this->schemaBuilder);
103
    }, $plugins));
104
  }
105
106
  /**
107
   * Retrieve all mutations.
108
   *
109
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
110
   *   The list of mutation plugins.
111
   */
112
  protected function getMutations() {
113
    return $this->schemaBuilder->getMutationMap();
114
  }
115
116
  /**
117
   * Retrieve all fields that are not associated with a specific type.
118
   *
119
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
120
   *   The list root field plugins.
121
   */
122
  protected function getRootFields() {
123
    return $this->getFields('Root');
124
  }
125
126
  /**
127
   * Retrieve all fields that are not associated with a specific type.
128
   *
129
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
130
   *   The list root field plugins.
131
   */
132
  protected function getFields($type) {
133
    $map = $this->schemaBuilder->getFieldMap();
134
    return isset($map[$type]) ? $map[$type] : [];
135
  }
136
137
  /**
138
   * Retrieve all types to be registered explicitly.
139
   *
140
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
141
   *   The list of types to be registered explicitly.
142
   */
143
  protected function getTypes() {
144
    return array_filter(array_map(function ($type) {
145
      return $this->schemaBuilder->getTypeByName($type);
146
    }, array_keys($this->schemaBuilder->getTypeMap())));
147
  }
148
149
}
150