Passed
Push — master ( 64d02f...fbdc2d )
by Tim
03:59
created

ModuleSet   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
c 9
b 0
f 0
dl 0
loc 135
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setTestCase() 0 4 1
A setDependencies() 0 4 1
A setType() 0 4 1
A initialize() 0 8 1
A setAvailable() 0 4 1
A setRequired() 0 4 1
D invokeTest() 0 40 9
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\Modules;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestCaseFactory as TestCaseFactory;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
9
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
10
{
11
    /**
12
     * @var array
13
     */
14
    private $required;
15
16
    /**
17
     * @var array
18
     */
19
    private $available;
20
 
21
    /**
22
     * @var array
23
     */
24
    private $dependencies;
25
26
    /**
27
     * @var string
28
     */
29
    private $type;
30
31
    /**
32
     * @var TestCaseFactory
33
     */
34
    private $testCase;
35
36
    /**
37
     * @param TestData $testData
38
     *
39
     * @return void
40
     */
41
    protected function initialize($testData)
42
    {
43
        $this->setRequired($testData->getInput('required'));
44
        $this->setAvailable($testData->getInput('available'));
45
        $this->setDependencies($testData->getInput('dependencies'));
46
        $this->setType($testData->getInput('type'));
47
        $this->setTestCase($testData->getInput('testCase'));
48
        $this->setCategory($this->type.' modules');
0 ignored issues
show
Bug introduced by
Are you sure $this->type of type null|mixed|array can be used in concatenation? ( Ignorable by Annotation )

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

48
        $this->setCategory(/** @scrutinizer ignore-type */ $this->type.' modules');
Loading history...
49
    }
50
51
    /**
52
     * @param array
53
     */
54
    private function setRequired($required)
55
    {
56
        assert(is_array($required));
57
        $this->required = $required;
58
    }
59
60
61
    /**
62
     * @param array
63
     */
64
    private function setAvailable($available)
65
    {
66
        assert(is_array($available));
67
        $this->available = $available;
68
    }
69
70
71
    /**
72
     * @param array
73
     */
74
    private function setDependencies($dependencies)
75
    {
76
        assert(is_array($dependencies));
77
        $this->dependencies = $dependencies;
78
    }
79
80
81
    /**
82
     * @param string
83
     */
84
    private function setType($type)
85
    {
86
        assert(is_string($type));
87
        $this->type = $type;
88
    }
89
90
91
    /**
92
     * @param TestCase
93
     */
94
    private function setTestCase($testCase)
95
    {
96
        assert($testCase instanceof TestCaseFactory);
97
        $this->testCase = $testCase;
98
    }
99
100
    
101
    /**
102
     * @return void
103
     */
104
    public function invokeTest()
105
    {
106
        if (empty($this->available)) {
107
            $state = State::SKIPPED;
108
        } else {
109
            foreach ($this->required as $module) {
110
                $testData = new TestData([
111
                    'required' => $module,
112
                    'available' => $this->available
113
                ]);
114
        
115
                $moduleTest = new $this->testCase($this, $testData);
116
                $moduleTestResult = $moduleTest->getTestResult();
117
                if ($moduleTestResult->getState() !== State::OK) {
118
                    $missing = array();
119
                    while ($dependency = array_search($this->required, $this->dependencies)) {
120
                        if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
121
                            $missing[] = $dependency;
122
                        }
123
                        unset($dependencies[$dependency]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dependencies does not exist. Did you maybe mean $dependency?
Loading history...
124
                    }
125
                    if (!empty($missing)) {
126
                        $moduleTestResult->setSubject($moduleTest->getModuleName());
127
                        $moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing));
128
                    }
129
                    $this->addTestResult($moduleTestResult);
130
                }
131
            }
132
             $state = $this->calculateState();
133
        }
134
135
        $testResult = new TestResult($this->type, implode(', ', $this->required));       
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...uite\Modules\TestResult 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...
136
        if ($state === State::OK) {
137
            $testResult->setMessage('All required modules are loaded');
138
        } elseif ($state === State::SKIPPED) {
139
            $testResult->setMessage('Unable to verify installed modules');
140
        } else {
141
            $testResult->setMessage('Not all required modules are loaded');
142
        }
143
        $this->setTestResult($testResult);
144
    }
145
}
146