Completed
Push — 8.x-3.x ( e282f0...b8749e )
by Sebastian
02:17 queued 57s
created

MenuByName::resolveValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 4
dl 7
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Entity\EntityTypeManagerInterface;
7
use Drupal\Core\Language\LanguageManagerInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\graphql\GraphQL\Execution\ResolveContext;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use Drupal\system\MenuInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use GraphQL\Type\Definition\ResolveInfo;
14
15
/**
16
 * Retrieve a menu by it's name.
17
 *
18
 * @GraphQLField(
19
 *   id = "menu_by_name",
20
 *   secure = true,
21
 *   name = "menuByName",
22
 *   description = @Translation("Loads a menu by its machine-readable name."),
23
 *   type = "Menu",
24
 *   arguments = {
25
 *     "name" = "String!",
26
 *     "language" = "LanguageId"
27
 *   },
28
 *   contextual_arguments = {"language"},
29
 *   response_cache_contexts = {"languages:language_interface"}
30
 * )
31
 */
32 View Code Duplication
class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
  use DependencySerializationTrait;
34
35
  /**
36
   * The entity type manager.
37
   *
38
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39
   */
40
  protected $entityTypeManager;
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
46
    return new static($configuration, $pluginId, $pluginDefinition, $container->get('entity_type.manager'));
47
  }
48
49
  /**
50
   * MenuByName 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.
58
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
59
   *   The entity type manager service.
60
   */
61
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
62
    parent::__construct($configuration, $pluginId, $pluginDefinition);
63
    $this->entityTypeManager = $entityTypeManager;
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
70
    $entity = $this->entityTypeManager->getStorage('menu')->load($args['name']);
71
72
    if ($entity instanceof MenuInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\system\MenuInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
      yield $entity;
74
    }
75
  }
76
77
}
78