getDerivativeDefinitionsFromFieldDefinition()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 42
rs 8.0555
cc 9
nc 5
nop 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\ListDataDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\ListDataDefinitionInterface 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\graphql\Utility\StringHelper;
9
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
10
11
class EntityFieldDeriver extends EntityFieldDeriverBase {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
17
    $itemDefinition = $fieldDefinition->getItemDefinition();
18
    if (!($itemDefinition instanceof ComplexDataDefinitionInterface) || !$propertyDefinitions = $itemDefinition->getPropertyDefinitions()) {
19
      return [];
20
    }
21
22
    $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
23
    $maxAge = $fieldDefinition->getCacheMaxAge();
24
    $contexts = $fieldDefinition->getCacheContexts();
25
26
    $entityTypeId = $fieldDefinition->getTargetEntityTypeId();
27
    $entityType = $this->entityTypeManager->getDefinition($entityTypeId);
28
    $supportsBundles = $entityType->hasKey('bundle');
29
    $fieldName = $fieldDefinition->getName();
30
    $fieldBundle = $fieldDefinition->getTargetBundle() ?: '';
31
32
    $derivative = [
33
      'parents' => [StringHelper::camelCase($entityTypeId, $supportsBundles ? $fieldBundle : '')],
34
      'name' => StringHelper::propCase($fieldName),
35
      'description' => $fieldDefinition->getDescription(),
36
      'field' => $fieldName,
37
      'schema_cache_tags' => $tags,
38
      'schema_cache_contexts' => $contexts,
39
      'schema_cache_max_age' => $maxAge,
40
    ] + $basePluginDefinition;
41
42
    if (count($propertyDefinitions) === 1) {
43
      $propertyDefinition = reset($propertyDefinitions);
44
      $derivative['type'] = $propertyDefinition->getDataType();
45
      $derivative['property'] = key($propertyDefinitions);
46
    }
47
    else {
48
      $derivative['type'] = StringHelper::camelCase('field', $entityTypeId, $supportsBundles ? $fieldBundle : '', $fieldName);
49
    }
50
51
    // Fields are usually multi-value. Simplify them for the schema if they are
52
    // configured for cardinality 1 (only works for configured fields).
53
    if (!(($storageDefinition = $fieldDefinition->getFieldStorageDefinition()) && !$storageDefinition->isMultiple())) {
54
      $derivative['type'] = StringHelper::listType($derivative['type']);
55
    }
56
57
    return ["$entityTypeId-$fieldName-$fieldBundle" => $derivative];
58
  }
59
}
60