Completed
Pull Request — 8.x-3.x (#409)
by Sebastian
02:31
created

EntityMutationInputTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B extractEntityInput() 0 23 4
A extractEntityFieldInput() 0 9 1
1
<?php
2
3
namespace Drupal\graphql_mutation\Plugin\GraphQL\Mutations;
4
5
use Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase;
6
use Drupal\graphql_mutation\Plugin\GraphQL\InputTypes\EntityInputField;
7
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
8
9
trait EntityMutationInputTrait {
10
11
  /**
12
   * Extract entity values from the resolver args.
13
   *
14
   * Loops over all input values and assigns them to their original field names.
15
   *
16
   * @param array $inputArgs
17
   *   The entity values provided through the resolver args.
18
   * @param \Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase $inputType
19
   *   The input type.
20
   *
21
   * @return array
22
   *   The extracted entity values with their proper, internal field names.
23
   */
24
  protected function extractEntityInput(array $inputArgs, InputTypePluginBase $inputType) {
25
    $fields = $inputType->getPluginDefinition()['fields'];
26
    return array_reduce(array_keys($inputArgs), function($carry, $current) use ($fields, $inputArgs, $inputType) {
27
      $isMulti = $fields[$current]['multi'];
28
      $fieldName = $fields[$current]['field_name'];
29
      $fieldValue = $inputArgs[$current];
30
      $fieldType = $inputType->getField($current)->getType()->getNamedType();
31
32
      if ($fieldType instanceof AbstractScalarType) {
33
        return $carry + [$fieldName => $fieldValue];
34
      }
35
36
      if ($fieldType instanceof EntityInputField) {
37
        $fieldValue = $isMulti ? array_map(function($value) use ($fieldType) {
38
          return $this->extractEntityFieldInput($value, $fieldType);
39
        }, $fieldValue) : $this->extractEntityFieldInput($fieldValue, $fieldType);
40
41
        return $carry + [$fieldName => $fieldValue];
42
      }
43
44
      return $carry;
45
    }, []);
46
  }
47
48
  /**
49
   * Extract property values from field values from the resolver args.
50
   *
51
   * Loops over all field properties and assigns them to their original property
52
   * names.
53
   *
54
   * @param array $fieldValue
55
   *   The field values keyed by property name.
56
   * @param \Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase $fieldType
57
   *   The field type.
58
   *
59
   * @return array
60
   *   The extracted field values with their proper, internal property names.
61
   */
62
  protected function extractEntityFieldInput(array $fieldValue, InputTypePluginBase $fieldType) {
63
    $properties = $fieldType->getPluginDefinition()['fields'];
64
    return array_reduce(array_keys($fieldValue), function($carry, $current) use ($properties, $fieldValue) {
65
      $key = $properties[$current]['property_name'];
66
      $value = $fieldValue[$current];
67
68
      return $carry + [$key => $value];
69
    }, []);
70
  }
71
72
}