Completed
Push — 8.x-3.x ( 58ea89...9f5f1b )
by Sebastian
02:22
created

CreateEntityDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 3
nop 1
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_mutation\Plugin\Deriver\Mutations;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Entity\ContentEntityTypeInterface;
7
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
10
use Drupal\graphql\Utility\StringHelper;
11
use Drupal\graphql_content_mutation\ContentEntityMutationSchemaConfig;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
class CreateEntityDeriver extends DeriverBase implements ContainerDeriverInterface {
15
  /**
16
   * The entity type manager service.
17
   *
18
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19
   */
20
  protected $entityTypeManager;
21
22
  /**
23
   * The entity manager service.
24
   *
25
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
26
   */
27
  protected $entityTypeBundleInfo;
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public static function create(ContainerInterface $container, $basePluginId) {
33
    return new static(
34
      $container->get('entity_type.bundle.info'),
35
      $container->get('entity_type.manager')
36
    );
37
  }
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function __construct(
43
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
44
    EntityTypeManagerInterface $entityTypeManager
45
  ) {
46
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
47
    $this->entityTypeManager = $entityTypeManager;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function getDerivativeDefinitions($basePluginDefinition) {
54
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $type) {
55
      if (!($type instanceof ContentEntityTypeInterface)) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\ContentEntityTypeInterface 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...
56
        continue;
57
      }
58
59
      foreach ($this->entityTypeBundleInfo->getBundleInfo($entityTypeId) as $bundleName => $bundle) {
60
        $this->derivatives["$entityTypeId:$bundleName"] = [
61
          'name' => StringHelper::propCase(['create', $entityTypeId, $bundleName]),
62
          'arguments' => [
63
            'input' => [
64
              'type' => StringHelper::camelCase([$entityTypeId, $bundleName, 'create', 'input']),
65
              'nullable' => FALSE,
66
              'multi' => FALSE,
67
            ],
68
          ],
69
          'entity_type' => $entityTypeId,
70
          'entity_bundle' => $bundleName,
71
        ] + $basePluginDefinition;
72
      }
73
    }
74
75
    return parent::getDerivativeDefinitions($basePluginDefinition);
76
  }
77
78
}
79