Completed
Pull Request — 8.x-3.x (#480)
by Sebastian
03:36
created

Route   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolveValues() 0 14 4
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing;
4
5
use Drupal\Component\Utility\UrlHelper;
6
use Drupal\Core\Url;
7
use Drupal\graphql\GraphQL\Cache\CacheableValue;
8
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
11
/**
12
 * Retrieve a route object based on a path.
13
 *
14
 * @GraphQLField(
15
 *   id = "url_route",
16
 *   secure = true,
17
 *   name = "route",
18
 *   description = @Translation("Loads a route by its path."),
19
 *   type = "Url",
20
 *   nullable = true,
21
 *   arguments = {
22
 *     "path" = "String"
23
 *   }
24
 * )
25
 */
26
class Route extends FieldPluginBase {
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...
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function resolveValues($value, array $args, ResolveInfo $info) {
32
    if (UrlHelper::isExternal($args['path'])) {
33
      yield Url::fromUri($args['path']);
34
    }
35
    else {
36
      $url = Url::fromUri("internal:{$args['path']}", ['routed_path' => $args['path']]);
37
      if ($url->isRouted() && $url->access()) {
38
        yield $url;
39
      }
40
      else {
41
        yield new CacheableValue(NULL, ['4xx-response']);
42
      }
43
    }
44
  }
45
46
}
47