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

TypePluginManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 114
loc 114
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 22 22 1
A getInstance() 7 7 2
A clearCachedDefinitions() 4 4 1
A setCachedDefinitions() 4 4 1
A getCacheTags() 10 10 2
A getCacheMaxAge() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class TypePluginManager extends DefaultPluginManager implements TypePluginManagerInterface {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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;
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