Completed
Pull Request — 8.x-3.x (#507)
by Sebastian
02:45 queued 26s
created

Path   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 8 1
A resolveValues() 0 12 4
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Language\LanguageManagerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\Core\Url;
9
use Drupal\graphql\GraphQL\Cache\CacheableValue;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
14
/**
15
 * @GraphQLField(
16
 *   id = "url_path",
17
 *   secure = true,
18
 *   name = "path",
19
 *   description = @Translation("The processed url path."),
20
 *   type = "String",
21
 *   parents = {"Url"},
22
 *   arguments = {
23
 *     "language" = "LanguageId"
24
 *   }
25
 * )
26
 */
27
class Path extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
28
  use DependencySerializationTrait;
29
30
  /**
31
   * The language manager service.
32
   *
33
   * @var \Drupal\Core\Language\LanguageManagerInterface
34
   */
35
  protected $languageManager;
36
37
  /**
38
   * Path constructor.
39
   *
40
   * @param array $configuration
41
   *   The plugin configuration array.
42
   * @param string $pluginId
43
   *   The plugin id.
44
   * @param mixed $pluginDefinition
45
   *   The plugin definition.
46
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
47
   *   The language manager service.
48
   */
49
  public function __construct(
50
    array $configuration,
51
    $pluginId,
52
    $pluginDefinition,
53
    LanguageManagerInterface $languageManager
54
  ) {
55
    parent::__construct($configuration, $pluginId, $pluginDefinition);
56
    $this->languageManager = $languageManager;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
63
    return new static(
64
      $configuration,
65
      $pluginId,
66
      $pluginDefinition,
67
      $container->get('language_manager')
68
    );
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function resolveValues($value, array $args, ResolveInfo $info) {
75
    if ($value instanceof Url) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Url 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...
76
      $language = isset($args['language']) ? $this->languageManager->getLanguage($args['language']) : NULL;
77
      if (!empty($language)) {
78
        // Copy the url object and set the language.
79
        $value = (clone $value)->setOption('language', $language);
80
      }
81
82
      $url = $value->toString(TRUE);
83
      yield new CacheableValue($url->getGeneratedUrl(), [$url]);
84
    }
85
  }
86
87
}
88