EntityQueryExclusive   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyBundleMode() 0 11 3
A getBaseQuery() 0 14 3
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\DependencyIn...dencySerializationTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Entity\ContentEntityInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\ContentEntityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Entity\Query\QueryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\Query\QueryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\graphql\GraphQL\Execution\ResolveContext;
9
use Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery\EntityQuery;
10
use GraphQL\Type\Definition\ResolveInfo;
11
12
/**
13
 * Query entities of the same type without the context's entity.
14
 *
15
 * @GraphQLField(
16
 *   id = "entity_query_exclusive",
17
 *   name = "entityQueryExclusive",
18
 *   secure = true,
19
 *   type = "EntityQueryResult!",
20
 *   parents = {"Entity"},
21
 *   arguments = {
22
 *     "filter" = "EntityQueryFilterInput",
23
 *     "sort" = "[EntityQuerySortInput]",
24
 *     "offset" = {
25
 *       "type" = "Int",
26
 *       "default" = 0
27
 *     },
28
 *     "limit" = {
29
 *       "type" = "Int",
30
 *       "default" = 10
31
 *     },
32
 *     "revisions" = {
33
 *       "type" = "EntityQueryRevisionMode",
34
 *       "default" = "default"
35
 *     },
36
 *     "bundles" = {
37
 *       "type" = "EntityQueryBundleMode",
38
 *       "default" = "same"
39
 *     }
40
 *   }
41
 * )
42
 */
43
class EntityQueryExclusive extends EntityQuery {
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  protected function getBaseQuery($value, array $args, ResolveContext $context, ResolveInfo $info) {
49
    if ($value instanceof ContentEntityInterface) {
50
      $type = $value->getEntityType();
51
      $id = $type->getKey('id');
52
53
      // Filter out the current entity.
54
      $query = parent::getBaseQuery($value, $args, $context, $info);
55
      $query->condition($id, $value->id(), '<>');
56
57
      if (array_key_exists('bundles', $args)) {
58
        $query = $this->applyBundleMode($query, $value, $args['bundles']);
0 ignored issues
show
Bug introduced by
It seems like $query can also be of type null; however, parameter $query of Drupal\graphql_core\Plug...sive::applyBundleMode() does only seem to accept Drupal\Core\Entity\Query\QueryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $query = $this->applyBundleMode(/** @scrutinizer ignore-type */ $query, $value, $args['bundles']);
Loading history...
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