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

ConfigEntityPropertyDeriver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 6 1
C getDerivativeDefinitions() 0 30 7
B getType() 0 29 6
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\Core\TypedData\ComplexDataDefinitionInterface;
11
use Drupal\Core\TypedData\ListDataDefinitionInterface;
12
use Drupal\Core\TypedData\Plugin\DataType\StringData;
13
use Drupal\Core\TypedData\PrimitiveInterface;
14
use Drupal\graphql\Utility\StringHelper;
15
use Drupal\graphql_core\Plugin\GraphQL\Interfaces\Entity\EntityType;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
class ConfigEntityPropertyDeriver extends DeriverBase implements ContainerDeriverInterface {
19
20
  const TYPEMAP = [
21
    'string' => 'String',
22
    'text' => 'String',
23
    'integer' => 'Int',
24
    'boolean' => 'Boolean',
25
  ];
26
27
  /**
28
   * The typed config manager service.
29
   *
30
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
31
   */
32
  protected $typedConfigManager;
33
34
  /**
35
   * The entity type manager service.
36
   *
37
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
38
   */
39
  protected $entityTypeManager;
40
41
  /**
42
   * ConfigFieldDeriver constructor.
43
   *
44
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
45
   *   The entity type manager service.
46
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
47
   *   The typed config manager service.
48
   */
49
  public function __construct(EntityTypeManagerInterface $entityTypeManager, TypedConfigManagerInterface $typedConfigManager) {
50
    $this->entityTypeManager = $entityTypeManager;
51
    $this->typedConfigManager = $typedConfigManager;
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public static function create(ContainerInterface $container, $basePluginId) {
58
    return new static(
59
      $container->get('entity_type.manager'),
60
      $container->get('config.typed')
61
    );
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function getDerivativeDefinitions($basePluginDefinition) {
68
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
69
      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...
70
        continue;
71
      }
72
73
      $prefix = $entityType->getConfigPrefix();
74
      if (!$this->typedConfigManager->hasDefinition("$prefix.*")) {
75
        continue;
76
      }
77
78
      $definition = $this->typedConfigManager->getDefinition("$prefix.*");
79
      if (empty($definition['mapping'])) {
80
        continue;
81
      }
82
83
      $export = array_diff_key($entityType->getPropertiesToExport() ?: [], ['_core' => TRUE]);
84
      $properties = array_intersect_key($definition['mapping'], $export);
85
      foreach ($properties as $propertyName => $propertyDefinition) {
86
        $this->derivatives["$entityTypeId:$propertyName"] = [
87
          'name' => StringHelper::propCase($propertyName),
88
          'type' => $this->getType($propertyDefinition),
89
          'parents' => [StringHelper::camelCase($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
105
   *   The type name.
106
   */
107
  protected function getType($propertyDefinition) {
108
    // References to other config schemas cannot be resolved at build-time. To
109
    // circumvent this problem, we simply return a generic union type shared by
110
    // all config types.
111
    if (!$this->typedConfigManager->hasDefinition($propertyDefinition['type'])) {
112
      return 'Config';
113
    }
114
115
    $dataDefinition = $this->typedConfigManager->buildDataDefinition($propertyDefinition, NULL);
116
    $dataClass = $dataDefinition->getClass();
117
    if ($dataClass instanceof PrimitiveInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\PrimitiveInterface 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...
118
      return 'String';
119
    }
120
121
    if ($dataDefinition instanceof ListDataDefinitionInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\ListDataDefinitionInterface 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...
122
      $foo = '';
0 ignored issues
show
Unused Code introduced by
$foo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123
    }
124
125
    if ($dataDefinition instanceof ComplexDataDefinitionInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\Co...DataDefinitionInterface 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...
126
      $foo = '';
0 ignored issues
show
Unused Code introduced by
$foo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
    }
128
129
    $type = $propertyDefinition['type'];
130
    if (!empty(self::TYPEMAP[$type])) {
131
      return self::TYPEMAP[$type];
132
    }
133
134
    return 'String';
135
  }
136
137
}