TypePluginManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 32
c 1
b 0
f 0
dl 0
loc 112
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheMaxAge() 0 15 4
A setCachedDefinitions() 0 3 1
A getInstance() 0 6 2
A getCacheTags() 0 9 2
A __construct() 0 21 1
A clearCachedDefinitions() 0 3 1
1
<?php
2
3
namespace Drupal\graphql\Plugin;
4
5
use Drupal\Core\Cache\Cache;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Cache\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Cache\CacheBackendInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Cache\CacheBackendInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Extension\ModuleHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Extension\ModuleHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Plugin\DefaultPluginManager;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\DefaultPluginManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class TypePluginManager extends DefaultPluginManager implements TypePluginManagerInterface {
11
12
  /**
13
   * Static cache of plugin instances.
14
   *
15
   * @var \Drupal\graphql\Plugin\TypePluginInterface[]
16
   */
17
  protected $instances;
18
19
  /**
20
   * TypePluginManager constructor.
21
   *
22
   * @param bool|string $pluginSubdirectory
23
   *   The plugin's subdirectory.
24
   * @param \Traversable $namespaces
25
   *   An object that implements \Traversable which contains the root paths
26
   *   keyed by the corresponding namespace to look for plugin implementations.
27
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
28
   *   The module handler.
29
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
30
   *   The cache backend.
31
   * @param string|null $pluginInterface
32
   *   The interface each plugin should implement.
33
   * @param string $pluginAnnotationName
34
   *   The name of the annotation that contains the plugin definition.
35
   * @param string $pluginType
36
   *   The plugin type.
37
   * @param array $config
38
   *   The configuration service parameter.
39
   */
40
  public function __construct(
41
    $pluginSubdirectory,
42
    \Traversable $namespaces,
43
    ModuleHandlerInterface $moduleHandler,
44
    CacheBackendInterface $cacheBackend,
45
    $pluginInterface,
46
    $pluginAnnotationName,
47
    $pluginType,
48
    array $config
49
  ) {
50
    parent::__construct(
51
      $pluginSubdirectory,
52
      $namespaces,
53
      $moduleHandler,
54
      $pluginInterface,
55
      $pluginAnnotationName
56
    );
57
58
    $this->alterInfo("graphql_{$pluginType}");
59
    $this->useCaches(empty($config['development']));
60
    $this->setCacheBackend($cacheBackend, $pluginType, ['graphql']);
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function getInstance(array $options) {
67
    if (!isset($this->instances[$options['id']])) {
68
      $this->instances[$options['id']] = $this->createInstance($options['id']);
69
    }
70
71
    return $this->instances[$options['id']];
72
  }
73
74
  /**
75
   * {@inheritdoc}
76
   */
77
  public function clearCachedDefinitions() {
78
    parent::clearCachedDefinitions();
79
    $this->instances = [];
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  protected function setCachedDefinitions($definitions) {
86
    $this->definitions = $definitions;
0 ignored issues
show
Bug Best Practice introduced by
The property definitions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
88
  }
89
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function getCacheTags() {
94
    $definitions = $this->getDefinitions();
95
    return array_reduce($definitions, function ($carry, $current) {
96
      if (!empty($current['schema_cache_tags'])) {
97
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
98
      }
99
100
      return $carry;
101
    }, $this->cacheTags);
102
  }
103
104
  /**
105
   * {@inheritdoc}
106
   */
107
  public function getCacheMaxAge() {
108
    $definitions = $this->getDefinitions();
109
    $age = Cache::PERMANENT;
110
    foreach ($definitions as $definition) {
111
      if (!isset($definition['schema_cache_max_age'])) {
112
        continue;
113
      }
114
115
      // Bail out early if the cache max age is 0.
116
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
117
        return $age;
118
      }
119
    }
120
121
    return $age;
122
  }
123
}
124