EntityFieldItemDeriver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
c 0
b 0
f 0
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getDerivativeDefinitionsFromFieldDefinition() 0 38 7
A extractDataType() 0 6 2
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Field\FieldDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\FieldDefinitionInterface 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\Core\TypedData\ComplexDataDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\Co...DataDefinitionInterface 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...
7
use Drupal\Core\TypedData\DataDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\DataDefinitionInterface 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...
8
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\Da...enceDefinitionInterface 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...
9
use Drupal\graphql\Utility\StringHelper;
10
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
11
12
class EntityFieldItemDeriver extends EntityFieldDeriverBase {
13
14
  /**
15
   * {@inheritdoc}
16
   */
17
  protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
18
    $itemDefinition = $fieldDefinition->getItemDefinition();
19
    if (!($itemDefinition instanceof ComplexDataDefinitionInterface) || !$propertyDefinitions = $itemDefinition->getPropertyDefinitions()) {
20
      return [];
21
    }
22
23
    if (count($propertyDefinitions) <= 1) {
24
      return [];
25
    }
26
27
    $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
28
    $contexts = $fieldDefinition->getCacheContexts();
29
    $maxAge = $fieldDefinition->getCacheMaxAge();
30
31
    $entityTypeId = $fieldDefinition->getTargetEntityTypeId();
32
    $entityType = $this->entityTypeManager->getDefinition($entityTypeId);
33
    $supportsBundles = $entityType->hasKey('bundle');
34
    $fieldName = $fieldDefinition->getName();
35
    $fieldBundle = $fieldDefinition->getTargetBundle() ?: '';
36
37
    $commonDefinition = [
38
      'parents' => [StringHelper::camelCase('field', $entityTypeId, $supportsBundles ? $fieldBundle : '', $fieldName)],
39
      'schema_cache_tags' => $tags,
40
      'schema_cache_contexts' => $contexts,
41
      'schema_cache_max_age' => $maxAge,
42
    ] + $basePluginDefinition;
43
44
    $derivatives = [];
45
    foreach ($propertyDefinitions as $property => $propertyDefinition) {
46
      $derivatives["$entityTypeId-$fieldName-$fieldBundle-$property"] = [
47
        'name' => StringHelper::propCase($property),
48
        'description' => $propertyDefinition->getDescription(),
49
        'property' => $property,
50
        'type' => $this->extractDataType($propertyDefinition),
51
      ] + $commonDefinition;
52
    }
53
54
    return $derivatives;
55
  }
56
57
  /**
58
   * Extracts the data type of a property's data definition.
59
   *
60
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $propertyDefinition
61
   *   The property's data definition.
62
   *
63
   * @return string
64
   *   The property's data type.
65
   */
66
  protected function extractDataType(DataDefinitionInterface $propertyDefinition) {
67
    if ($propertyDefinition instanceof DataReferenceDefinitionInterface) {
68
      return $propertyDefinition->getTargetDefinition()->getDataType();
69
    }
70
71
    return $propertyDefinition->getDataType();
72
  }
73
74
}
75