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

InputTypePluginBase::buildFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\InputTypes;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
7
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait;
8
use Drupal\graphql\Plugin\SchemaBuilderInterface;
9
use Drupal\graphql\Plugin\TypePluginInterface;
10
use Drupal\graphql\Plugin\TypePluginManager;
11
use Drupal\graphql\Utility\StringHelper;
12
use GraphQL\Type\Definition\InputObjectType;
13
14
abstract class InputTypePluginBase extends PluginBase implements TypePluginInterface {
15
  use DescribablePluginTrait;
16
  use TypedPluginTrait;
17
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
22
    return new InputObjectType([
23
      'name' => $definition['name'],
24
      'description' => $definition['description'],
25
      'fields' => function () use ($builder, $definition) {
26
        return $builder->processArguments($definition['fields']);
27
      },
28
    ]);
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34 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...
35
    $definition = $this->getPluginDefinition();
36
37
    return [
38
      'name' => $definition['name'],
39
      'description' => $this->buildDescription($definition),
40
      'fields' => $this->buildFields($definition),
41
    ];
42
  }
43
44
  /**
45
   * @param $definition
46
   *
47
   * @return array
48
   */
49
  protected function buildFields($definition) {
50
    return array_map(function ($field) use ($definition) {
51
      return [
52
        'type' => $this->buildFieldType($field, $definition),
53
        'description' => $this->buildFieldDescription($field, $definition),
54
        'default' => $this->buildFieldDefault($field, $definition),
55
      ];
56
    }, $definition['fields']);
57
  }
58
59
  /**
60
   * @param $field
61
   *
62
   * @return array
63
   */
64
  protected function buildFieldType($field) {
65
    $type = is_array($field) ? $field['type'] : $field;
66
    return StringHelper::parseType($type);
67
  }
68
69
  /**
70
   * @param $field
71
   * @param $definition
72
   *
73
   * @return string
74
   */
75
  protected function buildFieldDescription($field, $definition) {
76
    return (string) (isset($field['description']) ? $field['description'] : '');
77
  }
78
79
  /**
80
   * @param $field
81
   * @param $definition
82
   *
83
   * @return null
84
   */
85
  protected function buildFieldDefault($field, $definition) {
86
    return isset($field['default']) ? $field['default'] : NULL;
87
  }
88
}
89