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

CacheableEdgeTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getPlugin() 0 1 ?
A getSchemaCacheMetadata() 0 5 1
A getResponseCacheMetadata() 0 5 1
A getCacheMetadata() 0 17 4
1
<?php
2
3
namespace Drupal\graphql\GraphQL;
4
5
use Drupal\graphql\GraphQL\Schema\Schema;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
7
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface;
8
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
9
use Youshido\GraphQL\Schema\AbstractSchema;
10
11
trait CacheableEdgeTrait {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  abstract public function getPlugin(PluggableSchemaBuilderInterface $schemaBuilder);
17
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public function getSchemaCacheMetadata(AbstractSchema $schema) {
22
    return $this->getCacheMetadata($schema, function (TypeSystemPluginInterface $plugin) {
23
      return $plugin->getSchemaCacheMetadata();
24
    });
25
  }
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public function getResponseCacheMetadata(AbstractSchema $schema) {
31
    return $this->getCacheMetadata($schema, function (TypeSystemPluginInterface $plugin) {
32
      return $plugin->getResponseCacheMetadata();
33
    });
34
  }
35
36
  /**
37
   * Helper function to load cache metadata from a schema's edges.
38
   *
39
   * @param \Youshido\GraphQL\Schema\AbstractSchema $schema
40
   *   The schema.
41
   * @param callable $callback
42
   *   Callback to return the cache metadata from an edge.
43
   *
44
   * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface
45
   *   The cache metadata.
46
   */
47
  protected function getCacheMetadata(AbstractSchema $schema, callable $callback) {
48
    if (!$schema instanceof Schema) {
49
      return NULL;
50
    }
51
52
    $schemaPlugin = $schema->getSchemaPlugin();
53
    if (!$schemaPlugin instanceof PluggableSchemaPluginInterface) {
54
      return NULL;
55
    }
56
57
    /** @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface $plugin */
58
    if ($plugin = $this->getPlugin($schemaPlugin->getSchemaBuilder())) {
59
      return $callback($plugin);
60
    }
61
62
    return NULL;
63
  }
64
65
}