Completed
Pull Request — 8.x-3.x (#550)
by Philipp
02:10
created

EntityRevisionById::resolveValues()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 13
Ratio 44.83 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 4
nop 4
dl 13
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
4
5
use Drupal\Core\Cache\CacheableMetadata;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityRepositoryInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\Core\TypedData\TranslatableInterface;
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;
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 View Code Duplication
    else if (($access = $entity->access('view', NULL, TRUE)) && $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...
103
      if ($entity instanceof TranslatableInterface && isset($args['language']) && $args['language'] != $entity->language()->getId()) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\TranslatableInterface 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...
104
        $entity = $entity->getTranslation($args['language']);
105
      }
106
107
      yield new CacheableValue($entity, [$access]);
108
    }
109
    else {
110
      // If the entity exists but we do not grant access to it, we still want
111
      // to have it's cache metadata in the output because future changes to
112
      // the entity might affect its visibility for the user.
113
      yield new CacheableValue(NULL, [$access]);
114
    }
115
  }
116
117
}
118