LanguageSwitchLinks   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 28
c 1
b 0
f 1
dl 0
loc 80
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A create() 0 8 1
A resolveValues() 0 23 6
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\LanguageSwitch;
4
5
use Drupal\Core\Config\ConfigFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ConfigFactoryInterface 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\Language\LanguageInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Language\LanguageInterface 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\Language\LanguageManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Language\LanguageManagerInterface 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\Url;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Url 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\graphql\GraphQL\Buffers\SubRequestBuffer;
11
use Drupal\graphql\GraphQL\Execution\ResolveContext;
12
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
13
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...
14
use GraphQL\Type\Definition\ResolveInfo;
15
16
/**
17
 * @GraphQLField(
18
 *   id = "url_language_switch_links",
19
 *   secure = true,
20
 *   name = "languageSwitchLinks",
21
 *   type = "[LanguageSwitchLink]",
22
 *   parents = {"InternalUrl"},
23
 *   arguments = {
24
 *     "language" = "LanguageId"
25
 *   },
26
 *   response_cache_contexts = {
27
 *     "languages:language_url",
28
 *     "languages:language_interface",
29
 *   },
30
 *   contextual_arguments = {"language"}
31
 * )
32
 */
33
class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPluginInterface {
34
35
  /**
36
   * The language manager.
37
   *
38
   * @var \Drupal\Core\Language\LanguageManagerInterface
39
   */
40
  protected $languageManager;
41
42
  /**
43
   * The subrequest buffer service.
44
   *
45
   * @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer
46
   */
47
  protected $subRequestBuffer;
48
49
  /**
50
   * The config factory.
51
   *
52
   * @var \Drupal\Core\Config\ConfigFactoryInterface
53
   */
54
  protected $configFactory;
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60
    return new static(
61
      $configuration,
62
      $plugin_id,
63
      $plugin_definition,
64
      $container->get('language_manager'),
65
      $container->get('graphql.buffer.subrequest'),
66
      $container->get('config.factory')
67
    );
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function __construct(
74
    array $configuration,
75
    $pluginId,
76
    $pluginDefinition,
77
    LanguageManagerInterface $languageManager,
78
    SubRequestBuffer $subRequestBuffer,
79
    ConfigFactoryInterface $configFactory
80
  ) {
81
    parent::__construct($configuration, $pluginId, $pluginDefinition);
82
    $this->languageManager = $languageManager;
83
    $this->subRequestBuffer = $subRequestBuffer;
84
    $this->configFactory = $configFactory;
85
  }
86
87
  /**
88
   * {@inheritdoc}
89
   */
90
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
91
    if ($value instanceof Url) {
92
93
      // Use the <front> route if the requested url is the frontpage.
94
      $frontpage = $this->configFactory->get('system.site')->get('page.front');
95
      if ('/' . $value->getInternalPath() === $frontpage) {
96
        $value = Url::fromRoute('<front>');
97
      }
98
99
      $links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $value);
100
101
      $current = $this->languageManager->getLanguage($args['language']);
102
      if (!$current) {
103
        $current = $this->languageManager->getDefaultLanguage();
104
      }
105
106
      if (!empty($links->links)) {
107
        foreach ($links->links as $link) {
108
          // Yield the link array and the language object of the language
109
          // context resolved from the sub-request.
110
          yield [
111
            'link' => $link,
112
            'context' => $current,
113
          ];
114
        }
115
      }
116
    }
117
  }
118
119
}
120