EntityRevisionById::resolveValues()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 29
rs 8.0555
cc 9
nc 5
nop 4
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\Cache\CacheableMetadata;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Cache\CacheableMetadata 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\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...
7
use Drupal\Core\Entity\EntityRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityRepositoryInterface 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\Core\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface 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...
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\ContainerFactoryPluginInterface 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...
10
use Drupal\Core\TypedData\TranslatableInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\TypedData\TranslatableInterface 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...
11
use Drupal\graphql\GraphQL\Cache\CacheableValue;
12
use Drupal\graphql\GraphQL\Execution\ResolveContext;
13
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface 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...
15
use GraphQL\Type\Definition\ResolveInfo;
16
17
/**
18
 * @GraphQLField(
19
 *   id = "entity_revision_by_id",
20
 *   secure = true,
21
 *   arguments = {
22
 *     "id" = "String!"
23
 *   },
24
 *   contextual_arguments = {"language"},
25
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRevisionByIdDeriver"
26
 * )
27
 */
28
class EntityRevisionById extends FieldPluginBase implements ContainerFactoryPluginInterface {
29
  use DependencySerializationTrait;
30
31
  /**
32
   * The entity type manager service.
33
   *
34
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
35
   */
36
  protected $entityTypeManager;
37
38
  /**
39
   * The entity repository service.
40
   *
41
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
42
   */
43
  protected $entityRepository;
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
49
    return new static(
50
      $configuration,
51
      $pluginId,
52
      $pluginDefinition,
53
      $container->get('entity_type.manager'),
54
      $container->get('entity.repository')
55
    );
56
  }
57
58
  /**
59
   * EntityRevisionById constructor.
60
   *
61
   * @param array $configuration
62
   *   The plugin configuration array.
63
   * @param string $pluginId
64
   *   The plugin id.
65
   * @param mixed $pluginDefinition
66
   *   The plugin definition.
67
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
68
   *   The entity type manager service.
69
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
70
   *   The entity repository service.
71
   */
72
  public function __construct(
73
    array $configuration,
74
    $pluginId,
75
    $pluginDefinition,
76
    EntityTypeManagerInterface $entityTypeManager,
77
    EntityRepositoryInterface $entityRepository
78
  ) {
79
    parent::__construct($configuration, $pluginId, $pluginDefinition);
80
    $this->entityTypeManager = $entityTypeManager;
81
    $this->entityRepository = $entityRepository;
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
88
    $definition = $this->getPluginDefinition();
89
    $storage = $this->entityTypeManager->getStorage($definition['entity_type']);
90
91
    if (!$entity = $storage->loadRevision($args['id'])) {
92
      // If there is no entity with this id, add the list cache tags so that the
93
      // cache entry is purged whenever a new entity of this type is saved.
94
      $pluginDefinition = $this->getPluginDefinition();
95
      $entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']);
96
      $metadata = new CacheableMetadata();
97
      $metadata->addCacheTags($entityType->getListCacheTags());
98
99
      yield new CacheableValue(NULL, [$metadata]);
100
    }
101
    /** @var \Drupal\Core\Access\AccessResultInterface $access */
102
    else if (($access = $entity->access('view', NULL, TRUE)) && $access->isAllowed()) {
103
      if ($entity instanceof TranslatableInterface && isset($args['language']) && $args['language'] != $entity->language()->getId() && $entity->isTranslatable()) {
104
        if ($entity->hasTranslation($args['language'])) {
105
          $entity = $entity->getTranslation($args['language']);
106
        }
107
      }
108
109
      yield new CacheableValue($entity, [$access]);
110
    }
111
    else {
112
      // If the entity exists but we do not grant access to it, we still want
113
      // to have it's cache metadata in the output because future changes to
114
      // the entity might affect its visibility for the user.
115
      yield new CacheableValue(NULL, [$access]);
116
    }
117
  }
118
119
}
120