Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:22
created

MutationPluginManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 21 21 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\Cache\Context\CacheContextsManager;
8
use Drupal\Core\Extension\ModuleHandlerInterface;
9
use Drupal\Core\Plugin\DefaultPluginManager;
10
11 View Code Duplication
class MutationPluginManager extends DefaultPluginManager {
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...
12
13
  /**
14
   * Static cache of plugin instances.
15
   *
16
   * @var \Drupal\graphql\Plugin\MutationPluginInterface[]
17
   */
18
  protected $instances;
19
20
  /**
21
   * FieldPluginManager constructor.
22
   *
23
   * @param bool|string $pluginSubdirectory
24
   *   The plugin's subdirectory.
25
   * @param \Traversable $namespaces
26
   *   An object that implements \Traversable which contains the root paths
27
   *   keyed by the corresponding namespace to look for plugin implementations.
28
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
29
   *   The module handler.
30
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
31
   *   The cache backend.
32
   * @param string|null $pluginInterface
33
   *   The interface each plugin should implement.
34
   * @param string $pluginAnnotationName
35
   *   The name of the annotation that contains the plugin definition.
36
   */
37
  public function __construct(
38
    $pluginSubdirectory,
39
    \Traversable $namespaces,
40
    ModuleHandlerInterface $moduleHandler,
41
    CacheBackendInterface $cacheBackend,
42
    $pluginInterface,
43
    $pluginAnnotationName,
44
    array $config
45
  ) {
46
    parent::__construct(
47
      $pluginSubdirectory,
48
      $namespaces,
49
      $moduleHandler,
50
      $pluginInterface,
51
      $pluginAnnotationName
52
    );
53
54
    $this->alterInfo('graphql_mutations');
55
    $this->useCaches(empty($config['development']));
56
    $this->setCacheBackend($cacheBackend, 'mutations', ['graphql', 'graphql:mutations']);
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getInstance(array $options) {
63
    if (!isset($this->instances[$options['id']])) {
64
      $this->instances[$options['id']] = $this->createInstance($options['id']);
65
    }
66
67
    return $this->instances[$options['id']];
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function clearCachedDefinitions() {
74
    parent::clearCachedDefinitions();
75
    $this->instances = [];
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81
  protected function setCachedDefinitions($definitions) {
82
    $this->definitions = $definitions;
83
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function getCacheTags() {
90
    $definitions = $this->getDefinitions();
91
    return array_reduce($definitions, function ($carry, $current) {
92
      if (!empty($current['schema_cache_tags'])) {
93
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
94
      }
95
96
      return $carry;
97
    }, $this->cacheTags);
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103
  public function getCacheMaxAge() {
104
    $definitions = $this->getDefinitions();
105
    $age = Cache::PERMANENT;
106
    foreach ($definitions as $definition) {
107
      if (!isset($definition['schema_cache_max_age'])) {
108
        continue;
109
      }
110
111
      // Bail out early if the cache max age is 0.
112
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
113
        return $age;
114
      }
115
    }
116
117
    return $age;
118
  }
119
}
120