MenuLinks   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 15
c 0
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 2 1
A __construct() 0 3 1
A resolveValues() 0 13 6
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu;
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\Menu\MenuLinkInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuLinkInterface 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\Menu\MenuLinkTreeElement;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuLinkTreeElement 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\Menu\MenuLinkTreeInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuLinkTreeInterface 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\Menu\MenuTreeParameters;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuTreeParameters 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\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...
11
use Drupal\graphql\GraphQL\Execution\ResolveContext;
12
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
13
use Drupal\system\MenuInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\system\MenuInterface 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...
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
 * Retrieves a menus links.
19
 *
20
 * @GraphQLField(
21
 *   id = "menu_links",
22
 *   secure = true,
23
 *   name = "links",
24
 *   type = "[MenuLink]",
25
 *   parents = {"Menu"},
26
 *   response_cache_contexts = {"languages:language_url"}
27
 * )
28
 */
29
class MenuLinks extends FieldPluginBase implements ContainerFactoryPluginInterface {
30
  use DependencySerializationTrait;
31
32
  /**
33
   * The menu link tree.
34
   *
35
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
36
   */
37
  protected $menuLinkTree;
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
43
    return new static($configuration, $pluginId, $pluginDefinition, $container->get('menu.link_tree'));
44
  }
45
46
  /**
47
   * MenuLinks constructor.
48
   *
49
   * @param array $configuration
50
   *   The plugin configuration array.
51
   * @param string $pluginId
52
   *   The plugin id.
53
   * @param mixed $pluginDefinition
54
   *   The plugin definition.
55
   * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
56
   *   The menu link tree service.
57
   */
58
  public function __construct(array $configuration, $pluginId, $pluginDefinition, MenuLinkTreeInterface $menuLinkTree) {
59
    parent::__construct($configuration, $pluginId, $pluginDefinition);
60
    $this->menuLinkTree = $menuLinkTree;
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
67
    if ($value instanceof MenuInterface) {
68
      $tree = $this->menuLinkTree->load($value->id(), new MenuTreeParameters());
69
70
      $manipulators = [
71
        ['callable' => 'menu.default_tree_manipulators:checkAccess'],
72
        ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
73
      ];
74
75
      foreach (array_filter($this->menuLinkTree->transform($tree, $manipulators), function (MenuLinkTreeElement $item) {
76
        return $item->link instanceof MenuLinkInterface && $item->link->isEnabled() && (empty($item->access) || $item->access->isAllowed());
77
      }) as $branch) {
78
        yield $branch;
79
      }
80
    }
81
  }
82
83
}
84