Completed
Pull Request — 8.x-3.x (#571)
by Philipp
02:54
created

EntityQueryDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 20
rs 8.8571
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\Entity\ContentEntityTypeInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9
use Drupal\Core\StringTranslation\StringTranslationTrait;
10
use Drupal\Core\TypedData\TypedDataManager;
11
use Drupal\graphql\Utility\StringHelper;
12
use Drupal\graphql_core\Plugin\Deriver\EntityTypeDeriverBase;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class EntityQueryDeriver extends DeriverBase implements ContainerDeriverInterface {
16
  use StringTranslationTrait;
17
18
  /**
19
   * The entity type manager service.
20
   *
21
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22
   */
23
  protected $entityTypeManager;
24
25
  /**
26
   * The typed data manager service.
27
   *
28
   * @var \Drupal\Core\TypedData\TypedDataManager
29
   */
30
  protected $typedDataManager;
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public static function create(ContainerInterface $container, $basePluginId) {
36
    return new static(
37
      $container->get('entity_type.manager'),
38
      $container->get('typed_data_manager')
39
    );
40
  }
41
42
  /**
43
   * EntityQueryDeriver constructor.
44
   *
45
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
46
   *   The entity type manager service.
47
   * @param \Drupal\Core\TypedData\TypedDataManager $typedDataManager
48
   *   The typed data manager service.
49
   */
50
  public function __construct(
51
    EntityTypeManagerInterface $entityTypeManager,
52
    TypedDataManager $typedDataManager
53
  ) {
54
    $this->entityTypeManager = $entityTypeManager;
55
    $this->typedDataManager = $typedDataManager;
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function getDerivativeDefinitions($basePluginDefinition) {
62
    foreach ($this->entityTypeManager->getDefinitions() as $id => $type) {
63
      if ($type instanceof ContentEntityTypeInterface && EntityTypeDeriverBase::isAccessibleEntityType($type)) {
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...
64
        $derivative = [
65
          'name' => StringHelper::propCase($id, 'query'),
66
          'description' => $this->t("Loads '@type' entities.", ['@type' => $type->getLabel()]),
67
          'entity_type' => $id,
68
        ] + $basePluginDefinition;
69
70
        if ($id === 'node') {
71
          // TODO: Make this more generic.
72
          $derivative['response_cache_contexts'][] = 'user.node_grants:view';
73
        }
74
75
        $this->derivatives[$id] = $derivative;
76
      }
77
    }
78
79
    return parent::getDerivativeDefinitions($basePluginDefinition);
80
  }
81
82
}
83