DeleteEntityBase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 30
c 0
b 0
f 0
dl 0
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 35 4
A create() 0 7 1
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\EntityStorageException;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityStorageException 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\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...
12
use Drupal\graphql\GraphQL\Execution\ResolveContext;
13
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper;
14
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
15
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...
16
use GraphQL\Type\Definition\ResolveInfo;
17
18
abstract class DeleteEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface {
19
  use DependencySerializationTrait;
20
  use StringTranslationTrait;
21
22
  /**
23
   * The entity type manager.
24
   *
25
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26
   */
27
  protected $entityTypeManager;
28
29
  /**
30
   * The renderer service.
31
   *
32
   * @var \Drupal\Core\Render\RendererInterface
33
   */
34
  protected $renderer;
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
40
    return new static(
41
      $configuration,
42
      $pluginId,
43
      $pluginDefinition,
44
      $container->get('entity_type.manager'),
45
      $container->get('renderer')
46
    );
47
  }
48
49
  /**
50
   * DeleteEntityBase constructor.
51
   *
52
   * @param array $configuration
53
   *   The plugin configuration array.
54
   * @param string $pluginId
55
   *   The plugin id.
56
   * @param mixed $pluginDefinition
57
   *   The plugin definition array.
58
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
59
   *   The entity type manager service.
60
   * @param \Drupal\Core\Render\RendererInterface $renderer
61
   *   The renderer service.
62
   */
63
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer) {
64
    parent::__construct($configuration, $pluginId, $pluginDefinition);
65
    $this->entityTypeManager = $entityTypeManager;
66
    $this->renderer = $renderer;
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
73
    // There are cases where the Drupal entity API calls emit the cache metadata
74
    // in the current render context. In such cases
75
    // EarlyRenderingControllerWrapperSubscriber throws the leaked cache
76
    // metadata exception. To avoid this, wrap the execution in its own render
77
    // context.
78
    return $this->renderer->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) {
0 ignored issues
show
Unused Code introduced by
The import $context is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Unused Code introduced by
The import $info is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Unused Code introduced by
The import $value is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
79
      $entityTypeId = $this->pluginDefinition['entity_type'];
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 entity could not be loaded.'),
86
        ]);
87
      }
88
89
      if (!$entity->access('delete')) {
90
        return new EntityCrudOutputWrapper(NULL, NULL, [
91
          $this->t('You do not have the necessary permissions to delete this entity.'),
92
        ]);
93
      }
94
95
      try {
96
        $entity->delete();
97
      }
98
      catch (EntityStorageException $exception) {
99
        return new EntityCrudOutputWrapper(NULL, NULL, [
100
          $this->t('Entity deletion failed with exception: @exception.', [
101
            '@exception' => $exception->getMessage(),
102
          ]),
103
        ]);
104
      }
105
106
      return new EntityCrudOutputWrapper($entity);
107
    });
108
  }
109
110
}
111