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

EntityRevisionById::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 9
rs 9.6666
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\graphql\GraphQL\Buffers\EntityBuffer;
11
use Drupal\graphql\GraphQL\Cache\CacheableValue;
12
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Youshido\GraphQL\Execution\ResolveInfo;
15
16
/**
17
 * @GraphQLField(
18
 *   id = "entity_revision_by_id",
19
 *   secure = true,
20
 *   arguments = {
21
 *     "id" = "String!"
22
 *   },
23
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRevisionByIdDeriver"
24
 * )
25
 */
26
class EntityRevisionById 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 type manager service.
31
   *
32
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
33
   */
34
  protected $entityTypeManager;
35
36
  /**
37
   * The entity repository service.
38
   *
39
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
40
   */
41
  protected $entityRepository;
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function __construct(
47
    array $configuration,
48
    $pluginId,
49
    $pluginDefinition,
50
    EntityTypeManagerInterface $entityTypeManager,
51
    EntityRepositoryInterface $entityRepository
52
  ) {
53
    parent::__construct($configuration, $pluginId, $pluginDefinition);
54
    $this->entityTypeManager = $entityTypeManager;
55
    $this->entityRepository = $entityRepository;
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
62
    return new static(
63
      $configuration,
64
      $pluginId,
65
      $pluginDefinition,
66
      $container->get('entity_type.manager'),
67
      $container->get('entity.repository')
68
    );
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  protected function resolveValues($value, array $args, ResolveInfo $info) {
75
    $definition = $this->getPluginDefinition();
76
    $storage = $this->entityTypeManager->getStorage($definition['entity_type']);
77
78
    if (!$entity = $storage->loadRevision($args['id'])) {
79
      // If there is no entity with this id, add the list cache tags so that the
80
      // cache entry is purged whenever a new entity of this type is saved.
81
      $pluginDefinition = $this->getPluginDefinition();
82
      $entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']);
83
      $metadata = new CacheableMetadata();
84
      $metadata->addCacheTags($entityType->getListCacheTags());
85
86
      yield new CacheableValue(NULL, [$metadata]);
87
    }
88
    /** @var \Drupal\Core\Access\AccessResultInterface $access */
89 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...
90
      if (isset($args['language']) && $args['language'] != $entity->language()->getId()) {
91
        $entity = $this->entityRepository->getTranslationFromContext($entity, $args['language']);
92
      }
93
94
      yield new CacheableValue($entity, [$access]);
95
    }
96
    else {
97
      // If the entity exists but we do not grant access to it, we still want
98
      // to have it's cache metadata in the output because future changes to
99
      // the entity might affect its visibility for the user.
100
      yield new CacheableValue(NULL, [$access]);
101
    }
102
  }
103
104
}
105