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

ConfigEntityPropertyDeriver::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
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
  const TYPEMAP = [
17
    'string' => 'String',
18
    'text' => 'String',
19
    'integer' => 'Int',
20
    'boolean' => 'Boolean',
21
  ];
22
23
  /**
24
   * The typed config manager service.
25
   *
26
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
27
   */
28
  protected $typedConfigManager;
29
30
  /**
31
   * The entity type manager service.
32
   *
33
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
34
   */
35
  protected $entityTypeManager;
36
37
  /**
38
   * ConfigFieldDeriver constructor.
39
   *
40
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
41
   *   The entity type manager service.
42
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
43
   *   The typed config manager service.
44
   */
45
  public function __construct(EntityTypeManagerInterface $entityTypeManager, TypedConfigManagerInterface $typedConfigManager) {
46
    $this->entityTypeManager = $entityTypeManager;
47
    $this->typedConfigManager = $typedConfigManager;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public static function create(ContainerInterface $container, $basePluginId) {
54
    return new static(
55
      $container->get('entity_type.manager'),
56
      $container->get('config.typed')
57
    );
58
  }
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function getDerivativeDefinitions($basePluginDefinition) {
64
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
65
      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...
66
        continue;
67
      }
68
69
      $prefix = $entityType->getConfigPrefix();
70
      if (!$this->typedConfigManager->hasDefinition("$prefix.*")) {
71
        continue;
72
      }
73
74
      $definition = $this->typedConfigManager->getDefinition("$prefix.*");
75
      if (empty($definition['mapping'])) {
76
        continue;
77
      }
78
79
      $export = array_diff_key($entityType->getPropertiesToExport() ?: [], ['_core' => TRUE]);
80
      $properties = array_intersect_key($definition['mapping'], $export);
81
      foreach ($properties as $propertyName => $propertyDefinition) {
82
        if (!$type = $this->getType($propertyDefinition)) {
83
          continue;
84
        }
85
86
        $this->derivatives["$entityTypeId:$propertyName"] = [
87
          'name' => StringHelper::propCase($propertyName),
88
          'type' => $type,
89
          'parents' => [EntityType::getId($entityTypeId)],
90
          'property' => $propertyName,
91
        ] + $basePluginDefinition;
92
      }
93
    }
94
95
    return parent::getDerivativeDefinitions($basePluginDefinition);
96
  }
97
98
  /**
99
   * Derive the type name for a property definition.
100
   *
101
   * @param $propertyDefinition
102
   *   The property definition array.
103
   *
104
   * @return string|null
105
   *   The type name or NULL if no type could be determined.
106
   */
107
  protected function getType($propertyDefinition) {
108
    $type = $propertyDefinition['type'];
109
    if (!empty(self::TYPEMAP[$type])) {
110
      return self::TYPEMAP[$type];
111
    }
112
113
    return NULL;
114
  }
115
116
}