getDerivativeDefinitions()   B
last analyzed

Complexity

Conditions 9
Paths 3

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 50
rs 8.0555
cc 9
nc 3
nop 1
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Plugin\Derivative\DeriverBase 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\Entity\EntityFieldManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityFieldManagerInterface 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\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface 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\Entity\FieldableEntityInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\FieldableEntityInterface 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\Core\Field\BaseFieldDefinition;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\BaseFieldDefinition 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...
10
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\Disco...ntainerDeriverInterface 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...
11
use Drupal\Core\StringTranslation\StringTranslationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTransl...\StringTranslationTrait 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...
12
use Drupal\Core\TypedData\TypedDataManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\TypedDataManagerInterface 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...
13
use Drupal\graphql\Utility\StringHelper;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface 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...
15
16
class EntityReferenceQueryDeriver extends DeriverBase implements ContainerDeriverInterface {
17
  use StringTranslationTrait;
18
19
  /**
20
   * The entity type manager.
21
   *
22
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23
   */
24
  protected $entityTypeManager;
25
26
  /**
27
   * The entity field manager.
28
   *
29
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
30
   */
31
  protected $entityFieldManager;
32
33
  /**
34
   * The typed data manager service.
35
   *
36
   * @var \Drupal\Core\TypedData\TypedDataManagerInterface
37
   */
38
  protected $typedDataManager;
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public static function create(ContainerInterface $container, $basePluginId) {
44
    return new static(
45
      $container->get('entity_type.manager'),
46
      $container->get('entity_field.manager'),
47
      $container->get('typed_data_manager')
48
    );
49
  }
50
51
  /**
52
   * RawValueFieldItemDeriver constructor.
53
   *
54
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
55
   *   The entity type manager.
56
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
57
   *   The entity field manager.
58
   * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager
59
   *   The typed data manager service.
60
   */
61
  public function __construct(
62
    EntityTypeManagerInterface $entityTypeManager,
63
    EntityFieldManagerInterface $entityFieldManager,
64
    TypedDataManagerInterface $typedDataManager
65
  ) {
66
    $this->entityTypeManager = $entityTypeManager;
67
    $this->entityFieldManager = $entityFieldManager;
68
    $this->typedDataManager = $typedDataManager;
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function getDerivativeDefinitions($basePluginDefinition) {
75
    $fieldMap = $this->entityFieldManager->getFieldMap();
76
77
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
78
      $interfaces = class_implements($entityType->getClass());
79
      if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
80
        continue;
81
      }
82
83
      foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldDefinition) {
84
        if ($fieldDefinition->getType() !== 'entity_reference' || !$targetTypeId = $fieldDefinition->getSetting('target_type')) {
85
          continue;
86
        }
87
88
        $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
89
        $contexts = $fieldDefinition->getCacheContexts();
90
        $maxAge = $fieldDefinition->getCacheMaxAge();
91
92
        $targetType = $this->entityTypeManager->getDefinition($targetTypeId);
93
        $fieldName = $fieldDefinition->getName();
94
95
        if ($fieldDefinition instanceof BaseFieldDefinition || !$entityType->hasKey('bundle')) {
96
          $parents = [StringHelper::camelCase($entityTypeId)];
97
        }
98
        else {
99
          $parents = [];
100
          foreach ($fieldMap[$entityTypeId][$fieldName]['bundles'] as $bundle) {
101
            $parents[] = StringHelper::camelCase($entityTypeId . '_' . $bundle);
102
          }
103
        }
104
        $derivative = [
105
          'parents' => $parents,
106
          'name' => StringHelper::propCase('query', $fieldName),
107
          'description' => $this->t('Query reference: @description', [
108
            '@description' => $fieldDefinition->getDescription(),
109
          ]),
110
          'field' => $fieldName,
111
          'entity_key' => $targetType->getKey('id'),
112
          'entity_type' => $targetTypeId,
113
          'schema_cache_tags' => $tags,
114
          'schema_cache_contexts' => $contexts,
115
          'schema_cache_max_age' => $maxAge,
116
        ] + $basePluginDefinition;
117
118
        /** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $definition */
119
        $this->derivatives["$entityTypeId-$fieldName"] = $derivative;
0 ignored issues
show
Bug Best Practice introduced by
The property derivatives does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
120
      }
121
    }
122
123
    return $this->derivatives;
124
  }
125
}
126