Completed
Pull Request — 8.x-3.x (#477)
by Sebastian
04:03
created

getDerivativeDefinitions()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 6
nop 1
dl 0
loc 30
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
7
use Drupal\Core\Config\TypedConfigManagerInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
10
use Drupal\graphql\Utility\StringHelper;
11
use Drupal\graphql_core\Plugin\GraphQL\Interfaces\Entity\EntityType;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
class ConfigEntityPropertyDeriver extends DeriverBase implements ContainerDeriverInterface {
15
16
  /**
17
   * The typed config manager service.
18
   *
19
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
20
   */
21
  protected $typedConfigManager;
22
23
  /**
24
   * The entity type manager service.
25
   *
26
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27
   */
28
  protected $entityTypeManager;
29
30
  /**
31
   * ConfigFieldDeriver constructor.
32
   *
33
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
34
   *   The entity type manager service.
35
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
36
   *   The typed config manager service.
37
   */
38
  public function __construct(EntityTypeManagerInterface $entityTypeManager, TypedConfigManagerInterface $typedConfigManager) {
39
    $this->entityTypeManager = $entityTypeManager;
40
    $this->typedConfigManager = $typedConfigManager;
41
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public static function create(ContainerInterface $container, $basePluginId) {
47
    return new static(
48
      $container->get('entity_type.manager'),
49
      $container->get('config.typed')
50
    );
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function getDerivativeDefinitions($basePluginDefinition) {
57
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
58
      if (!$entityType instanceof ConfigEntityTypeInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Config\Entit...nfigEntityTypeInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
        continue;
60
      }
61
62
      $prefix = $entityType->getConfigPrefix();
63
      if (!$this->typedConfigManager->hasDefinition("$prefix.*")) {
64
        continue;
65
      }
66
67
      $export = array_diff_key($entityType->getPropertiesToExport() ?: [], ['_core' => TRUE]);
68
      $definition = $this->typedConfigManager->getDefinition("$prefix.*");
69
      $properties = array_intersect_key($definition['mapping'], $export);
70
      foreach ($properties as $propertyName => $propertyDefinition) {
71
        if ($propertyDefinition['type'] !== 'string') {
72
          continue;
73
        }
74
75
        $this->derivatives["$entityTypeId:$propertyName"] = [
76
          'name' => StringHelper::propCase($propertyName),
77
          'type' => 'String',
78
          'parents' => [EntityType::getId($entityTypeId)],
79
          'property' => $propertyName,
80
        ] + $basePluginDefinition;
81
      }
82
    }
83
84
    return parent::getDerivativeDefinitions($basePluginDefinition);
85
  }
86
87
}