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

BaseControl   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 35
c 1
b 0
f 0
dl 0
loc 112
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 2 1
A getControls() 0 10 3
A getInstance() 0 16 3
A getDependencies() 0 2 1
A __construct() 0 3 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\qa\Exportable;
7
use Drupal\qa\Pass;
8
use Drupal\qa\Plugin\QaCheckInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
abstract class BaseControl extends Exportable implements QaCheckInterface {
12
13
  /**
14
   * The package to which the control belongs
15
   *
16
   * @var string
17
   */
18
  public $package_name;
19
20
  /**
21
   * An options hash
22
   *
23
   * @var array
24
   */
25
  public $options;
26
27
  /**
28
   * The hash of passes for that control.
29
   *
30
   * @var array
31
   */
32
  public $passes;
33
34
  /**
35
   * Singleton-per-child-class data holder.
36
   *
37
   * @var array
38
   */
39
  protected static $instances = [];
40
41
  /**
42
   * Per-package list of instances
43
   *
44
   * @var array
45
   */
46
  protected static $packages = [];
47
48
  public function __construct() {
49
    parent::__construct();
50
    $this->package_name = $this->namespace;
51
  }
52
53
  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

53
  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...
54
    return new static();
55
  }
56
57
  /**
58
   * Return an array of module dependencies.
59
   *
60
   * @return array
61
   */
62
  public static function getDependencies() {
63
    return [];
64
  }
65
66
  /**
67
   * @return \Drupal\qa\Plugin\Qa\Control\BaseControl
68
   */
69
  public static function getInstance() {
70
    $name = get_called_class();
71
    if (!isset(self::$instances[$name])) {
72
      $instance = new $name();
73
      self::$instances[$name] = $instance;
74
      if (!isset(self::$packages[$instance->package_name])) {
75
        $package = new $instance->package_name();
76
        self::$packages[get_class($package)] = [
77
          'package' => $package,
78
          'controls' => [],
79
        ];
80
      }
81
      self::$packages[$instance->package_name]['controls'][$instance->name] = $instance;
82
    }
83
    $ret = self::$instances[$name];
84
    return $ret;
85
  }
86
87
  /**
88
   * Returns per-package controls.
89
   *
90
   * @param string $package_name
91
   *   If given, only return the list of controls belonging to that package
92
   *
93
   * @return array
94
   *   - if $package_name is given, an array of control instances
95
   *   - else a package-name-indexed hash of arrays of control instances
96
   */
97
  public static function getControls($package_name = NULL) {
98
    if (isset($package_name)) {
99
      $ret = isset(self::$packages[$package_name])
100
        ? self::$packages[$package_name]
101
        : NULL;
102
    }
103
    else {
104
      $ret = self::$packages;
105
    }
106
    return $ret;
107
  }
108
109
  /**
110
   * Run the control.
111
   *
112
   * @return \Drupal\qa\Pass
113
   *   - 0: failure
114
   *   - 1: success
115
   */
116
  public function run() {
117
    global $base_url;
118
    $site_key = Crypt::hmacBase64($base_url, \Drupal::service('private_key')->get());
119
    $key = uniqid($site_key);
120
    $pass = new Pass($this);
121
    $this->passes[$key] = $pass;
122
    return $pass;
123
  }
124
125
}
126