Completed
Pull Request — 8.x-3.x (#409)
by Sebastian
02:31
created

DeleteEntityBase::resolve()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 3
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\EntityStorageException;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\Core\StringTranslation\StringTranslationTrait;
10
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper;
11
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
15
abstract class DeleteEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface {
16
  use DependencySerializationTrait;
17
  use StringTranslationTrait;
18
19
  /**
20
   * The entity type manager.
21
   *
22
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23
   */
24
  protected $entityTypeManager;
25
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
30
    $this->entityTypeManager = $entityTypeManager;
31
    parent::__construct($configuration, $pluginId, $pluginDefinition);
32
  }
33
34
  /**
35
   * {@inheritdoc}
36
   */
37
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
38
    return new static(
39
      $configuration,
40
      $pluginId,
41
      $pluginDefinition,
42
      $container->get('entity_type.manager')
43
    );
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function resolve($value, array $args, ResolveInfo $info) {
50
    $entityTypeId = $this->pluginDefinition['entity_type'];
51
    $storage = $this->entityTypeManager->getStorage($entityTypeId);
52
53
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
54
    if (!$entity = $storage->load($args['id'])) {
55
      return new EntityCrudOutputWrapper(NULL, NULL, [
56
        $this->t('The requested entity could not be loaded.'),
57
      ]);
58
    }
59
60
    if (!$entity->access('delete')) {
61
      return new EntityCrudOutputWrapper(NULL, NULL, [
62
        $this->t('You do not have the necessary permissions to delete this entity.'),
63
      ]);
64
    }
65
66
    try {
67
      $entity->delete();
68
    }
69
    catch (EntityStorageException $exception) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\EntityStorageException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
70
      return new EntityCrudOutputWrapper(NULL, NULL, [
71
        $this->t('Entity deletion failed with exception: @exception.', [
72
          '@exception' => $exception->getMessage(),
73
        ]),
74
      ]);
75
    }
76
77
    return new EntityCrudOutputWrapper($entity);
78
  }
79
80
}
81