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

EnumPluginBase::buildEnumValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Enums;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
10
use GraphQL\Type\Definition\EnumType;
11
12
abstract class EnumPluginBase extends PluginBase implements TypeSystemPluginInterface {
13
  use DescribablePluginTrait;
14
  use CacheablePluginTrait;
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public static function createInstance(PluggableSchemaBuilder $builder, $definition, $id) {
20
    return new EnumType($definition);
21
  }
22
23
  /**
24
   * {@inheritdoc}
25
   */
26 View Code Duplication
  public function getDefinition() {
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...
27
    $definition = $this->getPluginDefinition();
28
29
    return [
30
      'name' => $definition['name'],
31
      'description' => $this->buildDescription($definition),
32
      'values' => $this->buildEnumValues($definition),
33
    ];
34
  }
35
36
  /**
37
   * @param $definition
38
   *
39
   * @return array
40
   */
41
  protected function buildEnumValues($definition) {
42
    return array_map(function ($value) use ($definition) {
43
      return [
44
        'value' => $this->buildEnumValue($value, $definition),
45
        'description' => $this->buildEnumDescription($value, $definition),
46
      ];
47
    }, $definition['values']);
48
  }
49
50
  /**
51
   * @param $value
52
   * @param $definition
53
   *
54
   * @return mixed
55
   */
56
  protected function buildEnumValue($value, $definition) {
57
    return is_array($value) ? $value['value'] : $value;
58
  }
59
60
  /**
61
   * @param $value
62
   * @param $definition
63
   *
64
   * @return string
65
   */
66
  protected function buildEnumDescription($value, $definition) {
67
    return (string) (is_array($value) ? $value['description'] : '');
68
  }
69
70
}
71