EntityById::resolveValues()   B
last analyzed

Complexity

Conditions 8
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 25
rs 8.4444
cc 8
nc 1
nop 4
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\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...
7
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...
8
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...
9
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...
10
use Drupal\graphql\GraphQL\Buffers\EntityBuffer;
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_by_id",
20
 *   secure = true,
21
 *   arguments = {
22
 *     "id" = "String!"
23
 *   },
24
 *   contextual_arguments = {"language"},
25
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityByIdDeriver"
26
 * )
27
 */
28
class EntityById extends FieldPluginBase implements ContainerFactoryPluginInterface {
29
  use DependencySerializationTrait;
30
31
  /**
32
   * The entity buffer service.
33
   *
34
   * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer
35
   */
36
  protected $entityBuffer;
37
38
  /**
39
   * The entity type manager service.
40
   *
41
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
42
   */
43
  protected $entityTypeManager;
44
45
  /**
46
   * The entity repository service.
47
   *
48
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
49
   */
50
  protected $entityRepository;
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
56
    return new static(
57
      $configuration,
58
      $pluginId,
59
      $pluginDefinition,
60
      $container->get('entity_type.manager'),
61
      $container->get('entity.repository'),
62
      $container->get('graphql.buffer.entity')
63
    );
64
  }
65
66
  /**
67
   * EntityById constructor.
68
   *
69
   * @param array $configuration
70
   *   The plugin configuration array.
71
   * @param string $pluginId
72
   *   The plugin id.
73
   * @param mixed $pluginDefinition
74
   *   The plugin definition.
75
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
76
   *   The entity type manager service.
77
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
78
   *   The entity repository service.
79
   * @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer
80
   *   The entity buffer service.
81
   */
82
  public function __construct(
83
    array $configuration,
84
    $pluginId,
85
    $pluginDefinition,
86
    EntityTypeManagerInterface $entityTypeManager,
87
    EntityRepositoryInterface $entityRepository,
88
    EntityBuffer $entityBuffer
89
  ) {
90
    parent::__construct($configuration, $pluginId, $pluginDefinition);
91
    $this->entityBuffer = $entityBuffer;
92
    $this->entityTypeManager = $entityTypeManager;
93
    $this->entityRepository = $entityRepository;
94
  }
95
96
  /**
97
   * {@inheritdoc}
98
   */
99
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
100
    $resolver = $this->entityBuffer->add($this->getPluginDefinition()['entity_type'], $args['id']);
101
    return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolver) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

101
    return function ($value, array $args, /** @scrutinizer ignore-unused */ ResolveContext $context, ResolveInfo $info) use ($resolver) {

This check looks for 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. ( Ignorable by Annotation )

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

101
    return function ($value, array $args, ResolveContext $context, /** @scrutinizer ignore-unused */ ResolveInfo $info) use ($resolver) {

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

Loading history...
102
      if (!$entity = $resolver()) {
103
        // If there is no entity with this id, add the list cache tags so that the
104
        // cache entry is purged whenever a new entity of this type is saved.
105
        $pluginDefinition = $this->getPluginDefinition();
106
        $entityType = $this->entityTypeManager->getDefinition($pluginDefinition['entity_type']);
107
        yield (new CacheableValue(NULL))->addCacheTags($entityType->getListCacheTags());
108
      }
109
      else {
110
        /** @var \Drupal\Core\Entity\EntityInterface $entity */
111
        $access = $entity->access('view', NULL, TRUE);
112
113
        if ($access->isAllowed()) {
114
          if (isset($args['language']) && $args['language'] != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->isTranslatable()) {
115
            if ($entity->hasTranslation($args['language'])) {
116
              $entity = $entity->getTranslation($args['language']);
117
            }
118
          }
119
120
          yield $entity->addCacheableDependency($access);
121
        }
122
        else {
123
          yield new CacheableValue(NULL, [$access]);
124
        }
125
      }
126
    };
127
  }
128
129
}
130