EnumPluginBase::buildEnumDescription()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Enums;
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\CacheablePluginTrait;
7
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
8
use Drupal\graphql\Plugin\SchemaBuilderInterface;
9
use Drupal\graphql\Plugin\TypePluginInterface;
10
use Drupal\graphql\Plugin\TypePluginManager;
11
use GraphQL\Type\Definition\EnumType;
12
13
abstract class EnumPluginBase extends PluginBase implements TypePluginInterface {
14
  use CacheablePluginTrait;
15
  use DescribablePluginTrait;
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
21
    return new EnumType([
22
      'name' => $definition['name'],
23
      'description' => $definition['description'],
24
      'values' => $definition['values'],
25
      'contexts' => $definition['contexts'],
26
    ]);
27
  }
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function getDefinition() {
33
    $definition = $this->getPluginDefinition();
34
35
    return [
36
      'name' => $definition['name'],
37
      'description' => $this->buildDescription($definition),
38
      'values' => $this->buildEnumValues($definition),
39
      'contexts' => $this->buildCacheContexts($definition),
40
    ];
41
  }
42
43
  /**
44
   * Builds the enum values.
45
   *
46
   * @param array $definition
47
   *   The plugin definition array/
48
   *
49
   * @return array
50
   *   The enum values.
51
   */
52
  protected function buildEnumValues($definition) {
53
    return array_map(function ($value) use ($definition) {
54
      return [
55
        'value' => $this->buildEnumValue($value, $definition),
56
        'description' => $this->buildEnumDescription($value, $definition),
57
      ];
58
    }, $definition['values']);
59
  }
60
61
  /**
62
   * Builds the value of an enum item.
63
   *
64
   * @param mixed $value
65
   *   The enum's value definition.
66
   * @param array $definition
67
   *   The plugin definition array.
68
   *
69
   * @return mixed
70
   *   The value of the enum item.
71
   */
72
  protected function buildEnumValue($value, $definition) {
73
    return is_array($value) ? $value['value'] : $value;
74
  }
75
76
  /**
77
   * Builds the description of an enum item.
78
   *
79
   * @param mixed $value
80
   *   The enum's value definition.
81
   * @param array $definition
82
   *   The plugin definition array.
83
   *
84
   * @return string
85
   *   The description of the enum item.
86
   */
87
  protected function buildEnumDescription($value, $definition) {
88
    return (string) (is_array($value) ? $value['description'] : '');
89
  }
90
91
}
92