Completed
Push — 8.x-3.x ( 118308...bac421 )
by Sebastian
02:11
created

Route::resolveValues()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 3
dl 0
loc 17
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\Cache\CacheableMetadata;
7
use Drupal\Core\Url;
8
use Drupal\graphql\GraphQL\Cache\CacheableValue;
9
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
10
use Youshido\GraphQL\Execution\ResolveInfo;
11
12
/**
13
 * Retrieve a route object based on a path.
14
 *
15
 * @GraphQLField(
16
 *   id = "url_route",
17
 *   secure = true,
18
 *   name = "route",
19
 *   description = @Translation("Loads a route by its path."),
20
 *   type = "Url",
21
 *   nullable = true,
22
 *   arguments = {
23
 *     "path" = "String"
24
 *   }
25
 * )
26
 */
27
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...
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function resolveValues($value, array $args, ResolveInfo $info) {
33
    if (UrlHelper::isExternal($args['path'])) {
34
      yield Url::fromUri($args['path']);
35
    }
36
    else {
37
      $url = Url::fromUri("internal:{$args['path']}", ['routed_path' => $args['path']]);
38
      if ($url->isRouted() && $url->access()) {
39
        yield $url;
40
      }
41
      else {
42
        $metadata = new CacheableMetadata();
43
        $metadata->addCacheTags(['4xx-response']);
44
45
        yield new CacheableValue(NULL, [$metadata]);
46
      }
47
    }
48
  }
49
50
}
51