Completed
Push — 8.x-1.x ( 4c4e07...db76fc )
by Frédéric G.
25s queued 10s
created

BaseControl   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A getControls() 0 10 3
A getInstance() 0 16 3
A getPluginDefinition() 0 2 1
A getPluginId() 0 2 1
A __construct() 0 4 1
A run() 0 7 1
1
<?php
2
3
namespace Drupal\qa\Plugin\Qa\Control;
4
5
use Drupal\Component\Utility\Crypt;
6
use Drupal\Core\PrivateKey;
7
use Drupal\qa\Exportable;
8
use Drupal\qa\Pass;
9
use Drupal\qa\Plugin\QaCheckInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Base class for legacy Controls.
14
 */
15
abstract class BaseControl extends Exportable implements QaCheckInterface {
16
17
  /**
18
   * The package to which the control belongs.
19
   *
20
   * @var string
21
   */
22
  // phpcs:ignore
23
  public $package_name;
24
25
  /**
26
   * An options hash.
27
   *
28
   * @var array
29
   */
30
  public $options;
31
32
  /**
33
   * The hash of passes for that control.
34
   *
35
   * @var array
36
   */
37
  public $passes;
38
39
  /**
40
   * Singleton-per-child-class data holder.
41
   *
42
   * @var array
43
   */
44
  protected static $instances = [];
45
46
  /**
47
   * Per-package list of instances.
48
   *
49
   * @var array
50
   */
51
  protected static $packages = [];
52
53
  /**
54
   * The private_key service.
55
   *
56
   * @var \Drupal\Core\PrivateKey
57
   */
58
  protected $pk;
59
60
  /**
61
   * BaseControl constructor.
62
   *
63
   * @param \Drupal\Core\PrivateKey $pk
64
   *   The private_ket service.
65
   */
66
  public function __construct(PrivateKey $pk) {
67
    parent::__construct();
68
    $this->package_name = $this->namespace;
69
    $this->pk = $pk;
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public static function create(ContainerInterface $container) {
76
    $pk = $container->get('private_key');
77
    assert($pk instanceof PrivateKey);
78
    return new static($pk);
79
  }
80
81
  /**
82
   * Return an array of module dependencies.
83
   *
84
   * @return array
85
   *   The names of the dependencies.
86
   */
87
  abstract public static function getDependencies(): array;
88
89
  /**
90
   * Get the singleton instance for the requested control.
91
   *
92
   * @return \Drupal\qa\Plugin\Qa\Control\BaseControl
93
   *   The instance.
94
   */
95
  public static function getInstance() {
96
    $name = get_called_class();
97
    if (!isset(self::$instances[$name])) {
98
      $instance = new $name();
99
      self::$instances[$name] = $instance;
100
      if (!isset(self::$packages[$instance->package_name])) {
101
        $package = new $instance->package_name();
102
        self::$packages[get_class($package)] = [
103
          'package' => $package,
104
          'controls' => [],
105
        ];
106
      }
107
      self::$packages[$instance->package_name]['controls'][$instance->name] = $instance;
108
    }
109
    $ret = self::$instances[$name];
110
    return $ret;
111
  }
112
113
  /**
114
   * {@inheritdoc}
115
   */
116
  public function getPluginId() {
117
    return self::getInstance()->name;
118
  }
119
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function getPluginDefinition() {
124
    return [];
125
  }
126
127
  /**
128
   * Returns per-package controls.
129
   *
130
   * @param string $package_name
131
   *   If given, only return the list of controls belonging to that package.
132
   *
133
   * @return array
134
   *   - if $package_name is given, an array of control instances
135
   *   - else a package-name-indexed hash of arrays of control instances
136
   */
137
  public static function getControls($package_name = NULL) {
138
    if (isset($package_name)) {
139
      $ret = isset(self::$packages[$package_name])
140
        ? self::$packages[$package_name]
141
        : NULL;
142
    }
143
    else {
144
      $ret = self::$packages;
145
    }
146
    return $ret;
147
  }
148
149
  /**
150
   * Run the control.
151
   *
152
   * @return \Drupal\qa\Pass
153
   *   - 0: failure
154
   *   - 1: success
155
   */
156
  public function run(): Pass {
157
    global $base_url;
158
    $site_key = Crypt::hmacBase64($base_url, $this->pk->get());
159
    $key = uniqid($site_key);
160
    $pass = new Pass($this);
161
    $this->passes[$key] = $pass;
162
    return $pass;
163
  }
164
165
}
166