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

Alias::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\InternalUrl;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Language\LanguageManagerInterface;
7
use Drupal\Core\Path\AliasManagerInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\Core\Url;
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 = "internal_url_path_alias",
17
 *   secure = true,
18
 *   name = "pathAlias",
19
 *   description = @Translation("The url's path alias if any."),
20
 *   type = "String",
21
 *   parents = {"InternalUrl"}
22
 * )
23
 */
24
class Alias 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...
25
  use DependencySerializationTrait;
26
27
  /**
28
   * Instance of an alias manager.
29
   *
30
   * @var \Drupal\Core\Path\AliasManagerInterface
31
   */
32
  protected $aliasManager;
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('path.alias_manager')
43
    );
44
  }
45
46
  /**
47
   * Alias 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\Path\AliasManagerInterface $aliasManager
56
   *   The alias manager service
57
   */
58
  public function __construct(
59
    array $configuration,
60
    $pluginId,
61
    $pluginDefinition,
62
    AliasManagerInterface $aliasManager
63
  ) {
64
    parent::__construct($configuration, $pluginId, $pluginDefinition);
65
    $this->aliasManager = $aliasManager;
66
  }
67
68
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public function resolveValues($value, array $args, ResolveInfo $info) {
73
    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...
74
      $internal = "/{$value->getInternalPath()}";
75
      $alias = $this->aliasManager->getAliasByPath($internal);
76
77
      // If the fetched alias is identical to the internal path, it means we do
78
      // not have a configured alias for this path.
79
      if ($internal !== $alias) {
80
        yield $alias;
81
      }
82
    }
83
  }
84
85
}
86