Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:09
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\PluggableSchemaBuilder;
7
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
8
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
9
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait;
10
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
11
use Drupal\graphql\Utility\StringHelper;
12
use GraphQL\Type\Definition\InputObjectType;
13
14
abstract class InputTypePluginBase extends PluginBase implements TypeSystemPluginInterface {
15
  use CacheablePluginTrait;
16
  use DescribablePluginTrait;
17
  use TypedPluginTrait;
18
19
  /**
20
   * {@inheritdoc}
21
   */
22
  public static function createInstance(PluggableSchemaBuilder $builder, $definition, $id) {
23
    return new InputObjectType([
24
      'fields' => function () use ($builder, $definition) {
25
        return $builder->resolveArgs($definition['fields']);
26
      },
27
    ] + $definition);
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33 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...
34
    $definition = $this->getPluginDefinition();
35
36
    return [
37
      'name' => $definition['name'],
38
      'description' => $this->buildDescription($definition),
39
      'fields' => $this->buildFields($definition),
40
    ];
41
  }
42
43
  /**
44
   * @param $definition
45
   *
46
   * @return array
47
   */
48
  protected function buildFields($definition) {
49
    return array_map(function ($field) use ($definition) {
50
      return [
51
        'type' => $this->buildFieldType($field, $definition),
52
        'description' => $this->buildFieldDescription($field, $definition),
53
        'default' => $this->buildFieldDefault($field, $definition),
54
      ];
55
    }, $definition['fields']);
56
  }
57
58
  /**
59
   * @param $field
60
   *
61
   * @return array
62
   */
63
  protected function buildFieldType($field) {
64
    $type = is_array($field) ? $field['type'] : $field;
65
    return StringHelper::parseType($type);
66
  }
67
68
  /**
69
   * @param $field
70
   * @param $definition
71
   *
72
   * @return string
73
   */
74
  protected function buildFieldDescription($field, $definition) {
75
    return (string) (isset($field['description']) ? $field['description'] : '');
76
  }
77
78
  /**
79
   * @param $field
80
   * @param $definition
81
   *
82
   * @return null
83
   */
84
  protected function buildFieldDefault($field, $definition) {
85
    return isset($field['default']) ? $field['default'] : NULL;
86
  }
87
}
88