Completed
Push — 8.x-3.x ( e67c0e...2e01b0 )
by Philipp
02:27
created

EntityQuery::getCacheDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery;
4
5
use Drupal\Core\Cache\CacheableMetadata;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\graphql\GraphQL\Schema\Schema;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
15
/**
16
 * Retrieve a list of entities through an entity query.
17
 *
18
 * @GraphQLField(
19
 *   id = "entity_query",
20
 *   secure = true,
21
 *   name = "entityQuery",
22
 *   type = "EntityQueryResult",
23
 *   nullable = false,
24
 *   weight = -1,
25
 *   arguments = {
26
 *     "offset" = {
27
 *       "type" = "Int",
28
 *       "nullable" = true,
29
 *       "default" = 0
30
 *     },
31
 *     "limit" = {
32
 *       "type" = "Int",
33
 *       "nullable" = true,
34
 *       "default" = 10
35
 *     }
36
 *   },
37
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityQueryDeriver"
38
 * )
39
 */
40
class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
41
  use DependencySerializationTrait;
42
43
  /**
44
   * The entity type manager.
45
   *
46
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
47
   */
48
  protected $entityTypeManager;
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
54
    $this->entityTypeManager = $entityTypeManager;
55
    parent::__construct($configuration, $pluginId, $pluginDefinition);
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
62
    return new static(
63
      $configuration,
64
      $pluginId,
65
      $pluginDefinition,
66
      $container->get('entity_type.manager')
67
    );
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  protected function getCacheDependencies($result, $value, array $args) {
74
    $entityTypeId = $this->pluginDefinition['entity_type'];
75
    $type = $this->entityTypeManager->getDefinition($entityTypeId);
76
77
    $metadata = new CacheableMetadata();
78
    $metadata->addCacheTags($type->getListCacheTags());
79
    $metadata->addCacheContexts($type->getListCacheContexts());
80
81
    return [$metadata];
82
  }
83
84
  /**
85
   * Retrieves the schema builder from the resolve info.
86
   *
87
   * @param \Youshido\GraphQL\Execution\ResolveInfo $info
88
   *   The resolve info object.
89
   *
90
   * @return \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface|null
91
   *   The corresponding plugin instance for this edge.
92
   */
93 View Code Duplication
  protected function getSchemaBuilder(ResolveInfo $info) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    $schema = isset($info) ? $info->getExecutionContext()->getSchema() : NULL;
95
    if (!$schema instanceof Schema) {
96
      return NULL;
97
    }
98
99
    $schemaPlugin = $schema->getSchemaPlugin();
100
    if (!$schemaPlugin instanceof PluggableSchemaPluginInterface) {
101
      return NULL;
102
    }
103
104
    return $schemaPlugin->getSchemaBuilder();
105
  }
106
107
  /**
108
   * {@inheritdoc}
109
   */
110
  public function resolveValues($value, array $args, ResolveInfo $info) {
111
    $schemaBuilder = $this->getSchemaBuilder($info);
112
    $entityTypeId = $this->pluginDefinition['entity_type'];
113
    $storage = $this->entityTypeManager->getStorage($entityTypeId);
114
    $type = $this->entityTypeManager->getDefinition($entityTypeId);
115
116
    $query = $storage->getQuery();
117
    $query->range($args['offset'], $args['limit']);
118
    $query->sort($type->getKey('id'));
119
120
    if (array_key_exists('filter', $args) && $args['filter']) {
121
      /** @var \Youshido\GraphQL\Type\Object\AbstractObjectType $filter */
122
      $filter = $info->getField()->getArgument('filter')->getType();
123
      /** @var \Drupal\graphql\GraphQL\Type\InputObjectType $filterType */
124
      $filterType = $filter->getNamedType();
125
      $filterFields = $filterType->getPlugin($schemaBuilder)->getPluginDefinition()['fields'];
0 ignored issues
show
Bug introduced by
It seems like $schemaBuilder defined by $this->getSchemaBuilder($info) on line 111 can be null; however, Drupal\graphql\Plugin\Gr...renceTrait::getPlugin() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
126
127
      foreach ($args['filter'] as $key => $arg) {
128
        $query->condition($filterFields[$key]['field_name'], $arg);
129
      }
130
    }
131
132
    yield $query;
133
  }
134
135
}
136