JsonPathToEntity   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 1
A __construct() 0 9 1
A resolveValues() 0 9 4
1
<?php
2
3
namespace Drupal\graphql_json\Plugin\GraphQL\Fields;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\EntityRepositoryInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\graphql\GraphQL\Execution\ResolveContext;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Extract Url objects from json paths.
14
 *
15
 * @GraphQLField(
16
 *   id = "json_path_entity",
17
 *   secure = true,
18
 *   name = "pathToEntity",
19
 *   type = "Entity",
20
 *   parents = {"JsonObject", "JsonList"},
21
 *   arguments={
22
 *     "type" = {
23
 *       "type" = "String"
24
 *     },
25
 *     "steps" = {
26
 *       "type" = "String",
27
 *       "multi" = true
28
 *     }
29
 *   }
30
 * )
31
 */
32
class JsonPathToEntity extends JsonPath implements ContainerFactoryPluginInterface {
33
  use DependencySerializationTrait;
34
35
  /**
36
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
37
   */
38
  protected $entityRepository;
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public static function create(
44
    ContainerInterface $container,
45
    array $configuration,
46
    $plugin_id,
47
    $plugin_definition
48
  ) {
49
    return new static(
50
      $configuration,
51
      $plugin_id,
52
      $plugin_definition,
53
      $container->get('entity.repository')
54
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function __construct(
61
    array $configuration,
62
    $pluginId,
63
    $pluginDefinition,
64
    EntityRepositoryInterface $entityRepository
65
  ) {
66
    $this->entityRepository = $entityRepository;
67
    parent::__construct($configuration, $pluginId, $pluginDefinition);
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
74
    foreach (parent::resolveValues($value, $args, $context, $info) as $item) {
75
      if ($entity = $this->entityRepository->loadEntityByUuid($args['type'], $item)) {
76
        if ($entity->access('view')) {
77
          yield $entity;
78
        }
79
      }
80
    }
81
  }
82
83
}
84