EntityRendered::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 5
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\ContentEntityInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\ContentEntityInterface 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\Render\RenderContext;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Render\RenderContext 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\Render\RendererInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Render\RendererInterface 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_rendered",
20
 *   name = "entityRendered",
21
 *   type = "String",
22
 *   secure = true,
23
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRenderedDeriver",
24
 * )
25
 */
26
class EntityRendered extends FieldPluginBase implements ContainerFactoryPluginInterface {
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 renderer service.
38
   *
39
   * @var \Drupal\Core\Render\RendererInterface
40
   */
41
  protected $renderer;
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
47
    return new static(
48
      $configuration,
49
      $pluginId,
50
      $pluginDefinition,
51
      $container->get('entity_type.manager'),
52
      $container->get('renderer')
53
    );
54
  }
55
56
  /**
57
   * EntityRendered constructor.
58
   *
59
   * @param array $configuration
60
   *   The plugin configuration array.
61
   * @param string $pluginId
62
   *   The plugin id.
63
   * @param mixed $pluginDefinition
64
   *   The plugin definition.
65
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
66
   *   The entity type manager service.
67
   * @param \Drupal\Core\Render\RendererInterface $renderer
68
   *   The renderer service.
69
   */
70
  public function __construct(
71
    array $configuration,
72
    $pluginId,
73
    $pluginDefinition,
74
    EntityTypeManagerInterface $entityTypeManager,
75
    RendererInterface $renderer
76
  ) {
77
    parent::__construct($configuration, $pluginId, $pluginDefinition);
78
    $this->entityTypeManager = $entityTypeManager;
79
    $this->renderer = $renderer;
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
86
    if ($value instanceof ContentEntityInterface) {
87
      $mode = isset($args['mode']) ? $args['mode'] : 'full';
88
      $language = $value->language()->getId();
89
      $builder = $this->entityTypeManager->getViewBuilder($value->getEntityTypeId());
90
      $view = $builder->view($value, $mode, $language);
91
92
      $context = new RenderContext();
93
      /** @var \GraphQL\Executor\ExecutionResult|\GraphQL\Executor\ExecutionResult[] $result */
94
      $result = $this->renderer->executeInRenderContext($context, function() use ($view) {
95
        return $this->renderer->render($view);
96
      });
97
98
      if (!$context->isEmpty()) {
99
        yield new CacheableValue((string) $result, [$context->pop()]);
100
      }
101
      else {
102
        yield (string) $result;
103
      }
104
    }
105
  }
106
107
}
108