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

BaseControl::getDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
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
9
abstract class BaseControl extends Exportable {
10
11
  /**
12
   * The package to which the control belongs
13
   *
14
   * @var string
15
   */
16
  public $package_name;
17
18
  /**
19
   * An options hash
20
   *
21
   * @var array
22
   */
23
  public $options;
24
25
  /**
26
   * The hash of passes for that control.
27
   *
28
   * @var array
29
   */
30
  public $passes;
31
32
  /**
33
   * Singleton-per-child-class data holder.
34
   *
35
   * @var array
36
   */
37
  protected static $instances = [];
38
39
  /**
40
   * Per-package list of instances
41
   *
42
   * @var array
43
   */
44
  protected static $packages = [];
45
46
  public function __construct() {
47
    parent::__construct();
48
    $this->package_name = $this->namespace;
49
  }
50
51
  /**
52
   * Return an array of module dependencies.
53
   *
54
   * @return array
55
   */
56
  public static function getDependencies() {
57
    return [];
58
  }
59
60
  /**
61
   * @return \Drupal\qa\BaseControl
0 ignored issues
show
Bug introduced by
The type Drupal\qa\BaseControl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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