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

ConfigEntityPropertyDeriver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 6 1
C getDerivativeDefinitions() 0 34 8
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
      if (empty($definition['mapping'])) {
70
        continue;
71
      }
72
73
      $properties = array_intersect_key($definition['mapping'], $export);
74
      foreach ($properties as $propertyName => $propertyDefinition) {
75
        if ($propertyDefinition['type'] !== 'string') {
76
          continue;
77
        }
78
79
        $this->derivatives["$entityTypeId:$propertyName"] = [
80
          'name' => StringHelper::propCase($propertyName),
81
          'type' => 'String',
82
          'parents' => [EntityType::getId($entityTypeId)],
83
          'property' => $propertyName,
84
        ] + $basePluginDefinition;
85
      }
86
    }
87
88
    return parent::getDerivativeDefinitions($basePluginDefinition);
89
  }
90
91
}