Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
05:14
created

SchemaPluginManager::getCacheMaxAge()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheBackendInterface;
7
use Drupal\Core\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Plugin\DefaultPluginManager;
9
10
class SchemaPluginManager extends DefaultPluginManager {
11
12
  /**
13
   * SchemaPluginManager constructor.
14
   *
15
   * @param bool|string $pluginSubdirectory
16
   *   The plugin's subdirectory.
17
   * @param \Traversable $namespaces
18
   *   An object that implements \Traversable which contains the root paths
19
   *   keyed by the corresponding namespace to look for plugin implementations.
20
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
21
   *   The module handler.
22
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
23
   *   The cache backend.
24
   * @param string|null $pluginInterface
25
   *   The interface each plugin should implement.
26
   * @param string $pluginAnnotationName
27
   *   The name of the annotation that contains the plugin definition.
28
   * @param array $config
29
   *   The configuration service parameter.
30
   */
31
  public function __construct(
32
    $pluginSubdirectory,
33
    \Traversable $namespaces,
34
    ModuleHandlerInterface $moduleHandler,
35
    CacheBackendInterface $cacheBackend,
36
    $pluginInterface,
37
    $pluginAnnotationName,
38
    array $config
39
  ) {
40
    parent::__construct(
41
      $pluginSubdirectory,
42
      $namespaces,
43
      $moduleHandler,
44
      $pluginInterface,
45
      $pluginAnnotationName
46
    );
47
48
    $this->alterInfo('graphql_schema');
49
    $this->useCaches(empty($config['development']));
50
    $this->setCacheBackend($cacheBackend, 'schemas', ['graphql']);
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  protected function setCachedDefinitions($definitions) {
57
    $this->definitions = $definitions;
58
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function getCacheTags() {
65
    $definitions = $this->getDefinitions();
66
    return array_reduce($definitions, function ($carry, $current) {
67
      if (!empty($current['schema_cache_tags'])) {
68
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
69
      }
70
71
      return $carry;
72
    }, $this->cacheTags);
73
  }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78
  public function getCacheMaxAge() {
79
    $definitions = $this->getDefinitions();
80
    $age = Cache::PERMANENT;
81
    foreach ($definitions as $definition) {
82
      if (!isset($definition['schema_cache_max_age'])) {
83
        continue;
84
      }
85
86
      // Bail out early if the cache max age is 0.
87
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
88
        return $age;
89
      }
90
    }
91
92
    return $age;
93
  }
94
}
95