Passed
Push — monitor-3.0.x ( f85cdc...51d4ea )
by Tim
03:29
created

ModuleSet::invokeTest()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 3
b 0
f 0
nc 2
nop 0
dl 0
loc 29
rs 9.2888
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\Modules;
4
5
use SimpleSAML\Module\monitor\State;
6
use SimpleSAML\Module\monitor\TestCase;
7
use SimpleSAML\Module\monitor\TestConfiguration;
8
use SimpleSAML\Module\monitor\TestData;
9
use SimpleSAML\Module\monitor\TestResult;
10
use Webmozart\Assert\Assert;
11
12
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
13
{
14
    /** @var array */
15
    private $required;
16
17
    /** @var array */
18
    private $available;
19
20
    /** @var array */
21
    private $dependencies;
22
23
    /** @var string */
24
    private $type;
25
26
27
    /**
28
     * @param \SimpleSAML\Module\monitor\TestConfiguration $configuration
29
     * @param \SimpleSAML\Module\monitor\TestData $testData
30
     */
31
    public function __construct(TestConfiguration $configuration, TestData $testData)
32
    {
33
        $required = $testData->getInputItem('required');
34
        $available = $testData->getInputItem('available');
35
        $dependencies = $testData->getInputItem('dependencies');
36
        $type = $testData->getInputItem('type');
37
38
        Assert::isArray($required);
39
        Assert::isArray($available);
40
        Assert::isArray($dependencies);
41
        Assert::string($type);
42
43
        $this->setRequired($required);
0 ignored issues
show
Bug introduced by
It seems like $required can also be of type null; however, parameter $required of SimpleSAML\Module\monito...oduleSet::setRequired() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

43
        $this->setRequired(/** @scrutinizer ignore-type */ $required);
Loading history...
44
        $this->setAvailable($available);
0 ignored issues
show
Bug introduced by
It seems like $available can also be of type null; however, parameter $available of SimpleSAML\Module\monito...duleSet::setAvailable() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

44
        $this->setAvailable(/** @scrutinizer ignore-type */ $available);
Loading history...
45
        $this->setDependencies($dependencies);
0 ignored issues
show
Bug introduced by
It seems like $dependencies can also be of type null; however, parameter $dependencies of SimpleSAML\Module\monito...eSet::setDependencies() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $this->setDependencies(/** @scrutinizer ignore-type */ $dependencies);
Loading history...
46
        $this->setType($type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of SimpleSAML\Module\monito...es\ModuleSet::setType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

46
        $this->setType(/** @scrutinizer ignore-type */ $type);
Loading history...
47
48
        $this->setCategory($this->type . ' modules');
49
50
        parent::__construct($configuration);
51
    }
52
53
54
    /**
55
     * @param array $required
56
     * @return void
57
     */
58
    private function setRequired(array $required): void
59
    {
60
        $this->required = $required;
61
    }
62
63
64
    /**
65
     * @param array $available
66
     * @return void
67
     */
68
    private function setAvailable(array $available): void
69
    {
70
        $this->available = $available;
71
    }
72
73
74
    /**
75
     * @param array $dependencies
76
     * @return void
77
     */
78
    private function setDependencies(array $dependencies): void
79
    {
80
        $this->dependencies = $dependencies;
81
    }
82
83
84
    /**
85
     * @param string $type
86
     * @return void
87
     */
88
    private function setType(string $type): void
89
    {
90
        $this->type = $type;
91
    }
92
93
94
    /**
95
     * @return void
96
     */
97
    public function invokeTest(): void
98
    {
99
        if (empty($this->available)) {
100
            $state = State::SKIPPED;
101
        } else {
102
            foreach ($this->required as $module) {
103
                $testData = new TestData([
104
                    'required' => $module,
105
                    'available' => $this->available,
106
                    'type' => $this->type,
107
                ]);
108
109
                $moduleTest = new TestCase\Module($testData);
110
                $moduleTestResult = $moduleTest->getTestResult();
111
                if ($moduleTestResult->getState() !== State::OK) {
112
                    $missing = $this->findMissingDependencies($module);
113
                    if (!empty($missing)) {
114
                        $moduleTestResult->setSubject($moduleTest->getModuleName());
115
                        $moduleTestResult->setMessage('Module not loaded; dependency for ' . implode(', ', $missing));
116
                    }
117
                }
118
                $this->addTestResult($moduleTestResult);
119
            }
120
            $state = $this->calculateState();
121
        }
122
123
        $testResult = new TestResult($this->type, implode(', ', $this->required));
124
        $testResult->setState($state);
125
        $this->setTestResult($testResult);
126
    }
127
128
129
    /**
130
     * @param \SimpleSAML\Module\monitor\TestResult $testResult
131
     *
132
     * return void
133
     */
134
    protected function setTestResult(TestResult $testResult): void
135
    {
136
        $state = $testResult->getState();
137
        if ($state === State::OK) {
138
            $testResult->setMessage('All required modules are loaded');
139
        } elseif ($state === State::SKIPPED) {
140
            $testResult->setMessage('Unable to verify installed modules');
141
        } else {
142
            $testResult->setMessage('Not all required modules are loaded');
143
        }
144
        parent::setTestResult($testResult);
145
    }
146
147
148
    /**
149
     * @param string $module
150
     *
151
     * @return array
152
     */
153
    private function findMissingDependencies(string $module): array
154
    {
155
        $dependencies = $this->dependencies;
156
        $missing = [];
157
        while ($dependency = array_search($module, $dependencies)) {
158
            if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
159
                $missing[] = $dependency;
160
            }
161
            unset($dependencies[$dependency]);
162
        }
163
        return $missing;
164
    }
165
}
166