Completed
Pull Request — 8.x-3.x (#507)
by Sebastian
07:24 queued 04:45
created

Alias::resolveValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 12
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\Path\AliasManagerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\Core\Url;
9
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
13
/**
14
 * @GraphQLField(
15
 *   id = "internal_url_path_alias",
16
 *   secure = true,
17
 *   name = "pathAlias",
18
 *   description = @Translation("The url's path alias if any."),
19
 *   type = "String",
20
 *   parents = {"InternalUrl"}
21
 * )
22
 */
23
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...
24
  use DependencySerializationTrait;
25
26
  /**
27
   * Instance of an alias manager.
28
   *
29
   * @var \Drupal\Core\Path\AliasManagerInterface
30
   */
31
  protected $aliasManager;
32
33
  /**
34
   * Alias constructor.
35
   *
36
   * @param array $configuration
37
   *   The plugin configuration array.
38
   * @param string $pluginId
39
   *   The plugin id.
40
   * @param mixed $pluginDefinition
41
   *   The plugin definition.
42
   * @param \Drupal\Core\Path\AliasManagerInterface $aliasManager
43
   *   The alias manager service
44
   */
45
  public function __construct(
46
    array $configuration,
47
    $pluginId,
48
    $pluginDefinition,
49
    AliasManagerInterface $aliasManager
50
  ) {
51
    parent::__construct($configuration, $pluginId, $pluginDefinition);
52
    $this->aliasManager = $aliasManager;
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
59
    return new static(
60
      $configuration,
61
      $pluginId,
62
      $pluginDefinition,
63
      $container->get('path.alias_manager')
64
    );
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function resolveValues($value, array $args, ResolveInfo $info) {
71
    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...
72
      $internal = "/{$value->getInternalPath()}";
73
      $alias = $this->aliasManager->getAliasByPath($internal);
74
75
      // If the fetched alias is identical to the internal path, it means we do
76
      // not have a configured alias for this path.
77
      if ($internal !== $alias) {
78
        yield $alias;
79
      }
80
    }
81
  }
82
83
}
84