Completed
Push — 8.x-3.x ( 202563...3f10a4 )
by Sebastian
08:14 queued 05:51
created

EntityQueryEntities::resolveFromRevisionIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
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\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\EntityTypeManagerInterface;
7
use Drupal\Core\Entity\Query\QueryInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\graphql\GraphQL\Buffers\EntityBuffer;
10
use Drupal\graphql\GraphQL\Cache\CacheableValue;
11
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
15
/**
16
 * Retrieve the entity result set of an entity query.
17
 *
18
 * @GraphQLField(
19
 *   id = "entity_query_entities",
20
 *   secure = true,
21
 *   name = "entities",
22
 *   type = "[Entity]",
23
 *   parents = {"EntityQueryResult"}
24
 * )
25
 */
26
class EntityQueryEntities 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...
27
  use DependencySerializationTrait;
28
29
  /**
30
   * The entity buffer service.
31
   *
32
   * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer
33
   */
34
  protected $entityBuffer;
35
36
  /**
37
   * The entity type manager service.
38
   *
39
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40
   */
41
  protected $entityTypeManager;
42
43
  /**
44
   * EntityQueryEntities constructor.
45
   *
46
   * @param array $configuration
47
   *   The plugin configuration array.
48
   * @param string $pluginId
49
   *   The plugin id.
50
   * @param mixed $pluginDefinition
51
   *   The plugin definition array.
52
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
53
   *   The entity type manager service.
54
   * @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer
55
   *   The entity buffer service.
56
   */
57
  public function __construct(
58
    array $configuration,
59
    $pluginId,
60
    $pluginDefinition,
61
    EntityTypeManagerInterface $entityTypeManager,
62
    EntityBuffer $entityBuffer
63
  ) {
64
    parent::__construct($configuration, $pluginId, $pluginDefinition);
65
    $this->entityBuffer = $entityBuffer;
66
    $this->entityTypeManager = $entityTypeManager;
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
73
    return new static(
74
      $configuration,
75
      $pluginId,
76
      $pluginDefinition,
77
      $container->get('entity_type.manager'),
78
      $container->get('graphql.buffer.entity')
79
    );
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function resolveValues($value, array $args, ResolveInfo $info) {
86
    if ($value instanceof QueryInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\Query\QueryInterface 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...
87
      $type = $value->getEntityTypeId();
88
      $result = $value->execute();
89
90
      if ($value->hasTag('revisions')) {
91
        return $this->resolveFromRevisionIds($type, array_keys($result));
92
      }
93
94
      // If this is a revision query, the version ids are the array keys.
95
      return $this->resolveFromEntityIds($type, array_values($result));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->resolveFro...array_values($result)); (Closure) is incompatible with the return type of the parent method Drupal\graphql\Plugin\Gr...uginBase::resolveValues of type Generator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
    }
97
  }
98
99
  /**
100
   * Resolves entities lazily through the entity buffer.
101
   *
102
   * @param string $type
103
   *   The entity type.
104
   * @param array $ids
105
   *   The entity ids to load.
106
   *
107
   * @return \Closure
108
   *   The deferred resolver.
109
   */
110
  protected function resolveFromEntityIds($type, $ids) {
111
    $resolve = $this->entityBuffer->add($type, $ids);
112
    return function($value, array $args, ResolveInfo $info) use ($resolve) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
      return $this->resolveEntities($resolve());
114
    };
115
  }
116
117
  /**
118
   * Resolves entity revisions.
119
   *
120
   * @param string $type
121
   *   The entity type.
122
   * @param array $ids
123
   *   The entity revision ids to load.
124
   *
125
   * @return \Generator
126
   *   The resolved revisions.
127
   */
128
  protected function resolveFromRevisionIds($type, $ids) {
129
    $storage = $this->entityTypeManager->getStorage($type);
130
    $entities = array_map(function ($id) use ($storage) {
131
      return $storage->loadRevision($id);
132
    }, $ids);
133
134
    return $this->resolveEntities($entities);
135
  }
136
137
  /**
138
   * Resolves entity objects and checks view permissions.
139
   *
140
   * @param array $entities
141
   *   The entities to resolve.
142
   *
143
   * @return \Generator
144
   *   The resolved entities.
145
   */
146
  protected function resolveEntities(array $entities) {
147
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
148
    foreach ($entities as $entity) {
149
      $access = $entity->access('view', NULL, TRUE);
150
151 View Code Duplication
      if ($access->isAllowed()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
        yield $entity->addCacheableDependency($access);
153
      }
154
      else {
155
        yield new CacheableValue(NULL, [$access]);
156
      }
157
    }
158
  }
159
160
}
161