UpdateEntityBase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 5
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\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\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...
7
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...
8
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...
9
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...
10
use Drupal\Core\StringTranslation\StringTranslationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTransl...\StringTranslationTrait 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\Execution\ResolveContext;
12
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper;
13
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
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
abstract class UpdateEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface {
18
  use DependencySerializationTrait;
19
  use StringTranslationTrait;
20
21
  /**
22
   * The entity type manager.
23
   *
24
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25
   */
26
  protected $entityTypeManager;
27
28
  /**
29
   * The renderer service.
30
   *
31
   * @var \Drupal\Core\Render\RendererInterface
32
   */
33
  protected $renderer;
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
39
    return new static(
40
      $configuration,
41
      $pluginId,
42
      $pluginDefinition,
43
      $container->get('entity_type.manager'),
44
      $container->get('renderer')
45
    );
46
  }
47
48
  /**
49
   * UpdateEntityBase constructor.
50
   *
51
   * @param array $configuration
52
   *   The plugin configuration array.
53
   * @param string $pluginId
54
   *   The plugin id.
55
   * @param mixed $pluginDefinition
56
   *   The plugin definition.
57
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
58
   *   The entity type manager service.
59
   * @param \Drupal\Core\Render\RendererInterface $renderer
60
   *   The renderer service.
61
   */
62
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer) {
63
    parent::__construct($configuration, $pluginId, $pluginDefinition);
64
    $this->entityTypeManager = $entityTypeManager;
65
    $this->renderer = $renderer;
66
  }
67
68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
72
    // There are cases where the Drupal entity API calls emit the cache metadata
73
    // in the current render context. In such cases
74
    // EarlyRenderingControllerWrapperSubscriber throws the leaked cache
75
    // metadata exception. To avoid this, wrap the execution in its own render
76
    // context.
77
    return $this->renderer->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) {
78
      $entityTypeId = $this->pluginDefinition['entity_type'];
79
      $bundleName = $this->pluginDefinition['entity_bundle'];
80
      $storage = $this->entityTypeManager->getStorage($entityTypeId);
81
82
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
83
      if (!$entity = $storage->load($args['id'])) {
84
        return new EntityCrudOutputWrapper(NULL, NULL, [
85
          $this->t('The requested @bundle could not be loaded.', ['@bundle' => $bundleName]),
86
        ]);
87
      }
88
89
      if (!$entity->bundle() === $bundleName) {
90
        return new EntityCrudOutputWrapper(NULL, NULL, [
91
          $this->t('The requested entity is not of the expected type @bundle.', ['@bundle' => $bundleName]),
92
        ]);
93
      }
94
95
      if (!$entity->access('update')) {
96
        return new EntityCrudOutputWrapper(NULL, NULL, [
97
          $this->t('You do not have the necessary permissions to update this @bundle.', ['@bundle' => $bundleName]),
98
        ]);
99
      }
100
101
      // The raw input needs to be converted to use the proper field and property
102
      // keys because we usually convert them to camel case when adding them to
103
      // the schema. Allow the other implementations to control this easily.
104
      $input = $this->extractEntityInput($value, $args, $context, $info);
105
106
      try {
107
        foreach ($input as $key => $value) {
108
          $entity->get($key)->setValue($value);
109
        }
110
      }
111
      catch (\InvalidArgumentException $exception) {
112
        return new EntityCrudOutputWrapper(NULL, NULL, [
113
          $this->t('The entity update failed with exception: @exception.', ['@exception' => $exception->getMessage()]),
114
        ]);
115
      }
116
117
      if (($violations = $entity->validate()) && $violations->count()) {
118
        return new EntityCrudOutputWrapper(NULL, $violations);
119
      }
120
121
      if (($status = $entity->save()) && $status === SAVED_UPDATED) {
0 ignored issues
show
Bug introduced by
The constant Drupal\graphql_core\Plug...ns\Entity\SAVED_UPDATED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
        return new EntityCrudOutputWrapper($entity);
123
      }
124
125
      return NULL;
126
    });
127
  }
128
129
  /**
130
   * Extract entity values from the resolver args.
131
   *
132
   * Loops over all input values and assigns them to their original field names.
133
   *
134
   * @param $value
135
   *   The parent value.
136
   * @param array $args
137
   *   The entity values provided through the resolver args.
138
   * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context
139
   *   The resolve context.
140
   * @param \GraphQL\Type\Definition\ResolveInfo $info
141
   *   The resolve info object.
142
   *
143
   * @return array
144
   *   The extracted entity values with their proper, internal field names.
145
   */
146
  abstract protected function extractEntityInput($value, array $args, ResolveContext $context, ResolveInfo $info);
147
148
}
149