Completed
Push — 8.x-3.x ( a804f1...758c5e )
by Sebastian
14:55 queued 08:53
created

EntityQueryExclusive::getBaseQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\ContentEntityInterface;
7
use Drupal\Core\Entity\Query\QueryInterface;
8
use Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery\EntityQuery;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
11
/**
12
 * Query entities of the same type without the context's entity.
13
 *
14
 * @GraphQLField(
15
 *   id = "entity_query_exclusive",
16
 *   name = "entityQueryExclusive",
17
 *   secure = true,
18
 *   type = "EntityQueryResult!",
19
 *   parents = {"Entity"},
20
 *   arguments = {
21
 *     "filter" = "EntityQueryFilterInput",
22
 *     "sort" = "[EntityQuerySortInput]",
23
 *     "offset" = {
24
 *       "type" = "Int",
25
 *       "default" = 0
26
 *     },
27
 *     "limit" = {
28
 *       "type" = "Int",
29
 *       "default" = 10
30
 *     },
31
 *     "revisions" = {
32
 *       "type" = "EntityQueryRevisionMode",
33
 *       "default" = "default"
34
 *     },
35
 *     "bundles" = {
36
 *       "type" = "EntityQueryBundleMode",
37
 *       "default" = "same"
38
 *     }
39
 *   }
40
 * )
41
 */
42
class EntityQueryExclusive extends EntityQuery {
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...
43
  use DependencySerializationTrait;
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  protected function getBaseQuery($value, array $args, ResolveInfo $info) {
49
    if ($value instanceof ContentEntityInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\ContentEntityInterface 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...
50
      $type = $value->getEntityType();
51
      $id = $type->getKey('id');
52
53
      // Filter out the current entity.
54
      $query = parent::getBaseQuery($value, $args, $info);
55
      $query->condition($id, $value->id(), '<>');
56
57
      if (array_key_exists('bundles', $args)) {
58
        $query = $this->applyBundleMode($query, $value, $args['bundles']);
59
      }
60
61
      return $query;
62
    }
63
  }
64
65
  /**
66
   * Apply the specified bundle filtering mode to the query.
67
   *
68
   * @param \Drupal\Core\Entity\Query\QueryInterface $query
69
   *   The entity query object.
70
   * @param \Drupal\Core\Entity\ContentEntityInterface $value
71
   *   The parent entity object.
72
   * @param mixed $mode
73
   *   The revision query mode.
74
   *
75
   * @return \Drupal\Core\Entity\Query\QueryInterface The entity query object.
76
   * The entity query object.
77
   */
78
  protected function applyBundleMode(QueryInterface $query, ContentEntityInterface $value, $mode) {
79
    if ($mode === 'same') {
80
      $type = $value->getEntityType();
81
82
      if ($type->hasKey('bundle')) {
83
        $bundle = $type->getKey('bundle');
84
        $query->condition($bundle, $value->bundle());
85
      }
86
    }
87
88
    return $query;
89
  }
90
91
}
92