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

Route::resolveValues()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 3
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
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