Passed
Pull Request — 8.x-1.x (#17)
by Frédéric G.
05:23
created

BaseControl::getControls()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Drupal\qa\Plugin\Qa\Control;
4
5
use Drupal\Component\Utility\Crypt;
6
use Drupal\qa\Exportable;
7
use Drupal\qa\Pass;
8
use Drupal\qa\Plugin\QaCheckInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Base class for legacy Controls.
13
 */
14
abstract class BaseControl extends Exportable implements QaCheckInterface {
15
16
  /**
17
   * The package to which the control belongs.
18
   *
19
   * @var string
20
   */
21
  // phpcs:ignore
22
  public $package_name;
23
24
  /**
25
   * An options hash.
26
   *
27
   * @var array
28
   */
29
  public $options;
30
31
  /**
32
   * The hash of passes for that control.
33
   *
34
   * @var array
35
   */
36
  public $passes;
37
38
  /**
39
   * Singleton-per-child-class data holder.
40
   *
41
   * @var array
42
   */
43
  protected static $instances = [];
44
45
  /**
46
   * Per-package list of instances.
47
   *
48
   * @var array
49
   */
50
  protected static $packages = [];
51
52
  /**
53
   * BaseControl constructor.
54
   */
55
  public function __construct() {
56
    parent::__construct();
57
    $this->package_name = $this->namespace;
58
  }
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public static function create(ContainerInterface $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
  public static function create(/** @scrutinizer ignore-unused */ ContainerInterface $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    return new static();
65
  }
66
67
  /**
68
   * Return an array of module dependencies.
69
   *
70
   * @return array
71
   *   The names of the dependencies.
72
   */
73
  abstract public static function getDependencies(): array;
74
75
  /**
76
   * Get the singleton instance for the requested control.
77
   *
78
   * @return \Drupal\qa\Plugin\Qa\Control\BaseControl
79
   *   The instance.
80
   */
81
  public static function getInstance() {
82
    $name = get_called_class();
83
    if (!isset(self::$instances[$name])) {
84
      $instance = new $name();
85
      self::$instances[$name] = $instance;
86
      if (!isset(self::$packages[$instance->package_name])) {
87
        $package = new $instance->package_name();
88
        self::$packages[get_class($package)] = [
89
          'package' => $package,
90
          'controls' => [],
91
        ];
92
      }
93
      self::$packages[$instance->package_name]['controls'][$instance->name] = $instance;
94
    }
95
    $ret = self::$instances[$name];
96
    return $ret;
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function getPluginId() {
103
    return self::getInstance()->name;
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function getPluginDefinition() {
110
    return [];
111
  }
112
113
  /**
114
   * Returns per-package controls.
115
   *
116
   * @param string $package_name
117
   *   If given, only return the list of controls belonging to that package.
118
   *
119
   * @return array
120
   *   - if $package_name is given, an array of control instances
121
   *   - else a package-name-indexed hash of arrays of control instances
122
   */
123
  public static function getControls($package_name = NULL) {
124
    if (isset($package_name)) {
125
      $ret = isset(self::$packages[$package_name])
126
        ? self::$packages[$package_name]
127
        : NULL;
128
    }
129
    else {
130
      $ret = self::$packages;
131
    }
132
    return $ret;
133
  }
134
135
  /**
136
   * Run the control.
137
   *
138
   * @return \Drupal\qa\Pass
139
   *   - 0: failure
140
   *   - 1: success
141
   */
142
  public function run(): Pass {
143
    global $base_url;
144
    $site_key = Crypt::hmacBase64($base_url, \Drupal::service('private_key')->get());
145
    $key = uniqid($site_key);
146
    $pass = new Pass($this);
147
    $this->passes[$key] = $pass;
148
    return $pass;
149
  }
150
151
}
152