Completed
Push — 8.x-3.x ( 4bc053...679bcf )
by Sebastian
16:30 queued 06:25
created

SubscriptionPluginManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 111
loc 111
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\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Plugin\DefaultPluginManager;
9
10 View Code Duplication
class SubscriptionPluginManager 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...
11
12
  /**
13
   * Static cache of plugin instances.
14
   *
15
   * @var \Drupal\graphql\Plugin\SubscriptionPluginInterface[]
16
   */
17
  protected $instances;
18
19
  /**
20
   * FieldPluginManager 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 array $config
36
   *   The configuration service parameter.
37
   */
38
  public function __construct(
39
    $pluginSubdirectory,
40
    \Traversable $namespaces,
41
    ModuleHandlerInterface $moduleHandler,
42
    CacheBackendInterface $cacheBackend,
43
    $pluginInterface,
44
    $pluginAnnotationName,
45
    array $config
46
  ) {
47
    parent::__construct(
48
      $pluginSubdirectory,
49
      $namespaces,
50
      $moduleHandler,
51
      $pluginInterface,
52
      $pluginAnnotationName
53
    );
54
55
    $this->alterInfo('graphql_subscriptions');
56
    $this->useCaches(empty($config['development']));
57
    $this->setCacheBackend($cacheBackend, 'subscriptions', ['graphql']);
58
  }
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function getInstance(array $options) {
64
    if (!isset($this->instances[$options['id']])) {
65
      $this->instances[$options['id']] = $this->createInstance($options['id']);
66
    }
67
68
    return $this->instances[$options['id']];
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function clearCachedDefinitions() {
75
    parent::clearCachedDefinitions();
76
    $this->instances = [];
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  protected function setCachedDefinitions($definitions) {
83
    $this->definitions = $definitions;
84
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
85
  }
86
87
  /**
88
   * {@inheritdoc}
89
   */
90
  public function getCacheTags() {
91
    $definitions = $this->getDefinitions();
92
    return array_reduce($definitions, function ($carry, $current) {
93
      if (!empty($current['schema_cache_tags'])) {
94
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
95
      }
96
97
      return $carry;
98
    }, $this->cacheTags);
99
  }
100
101
  /**
102
   * {@inheritdoc}
103
   */
104
  public function getCacheMaxAge() {
105
    $definitions = $this->getDefinitions();
106
    $age = Cache::PERMANENT;
107
    foreach ($definitions as $definition) {
108
      if (!isset($definition['schema_cache_max_age'])) {
109
        continue;
110
      }
111
112
      // Bail out early if the cache max age is 0.
113
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
114
        return $age;
115
      }
116
    }
117
118
    return $age;
119
  }
120
}
121