ModuleSet::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\monitor\TestSuite\Modules;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\Module;
9
use SimpleSAML\Module\monitor\State;
10
use SimpleSAML\Module\monitor\TestCase;
11
use SimpleSAML\Module\monitor\TestConfiguration;
12
use SimpleSAML\Module\monitor\TestData;
13
use SimpleSAML\Module\monitor\TestResult;
14
15
use function array_search;
16
17
final class ModuleSet extends \SimpleSAML\Module\monitor\TestSuiteFactory
18
{
19
    /** @var array */
20
    private array $required;
21
22
    /** @var array */
23
    private array $available;
24
25
    /** @var array */
26
    private array $dependencies;
27
28
    /** @var string */
29
    private string $type;
30
31
32
    /**
33
     * @param \SimpleSAML\Module\monitor\TestConfiguration $configuration
34
     * @param \SimpleSAML\Module\monitor\TestData $testData
35
     */
36
    public function __construct(TestConfiguration $configuration, TestData $testData)
37
    {
38
        $this->setRequired($testData->getInputItem('required'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('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

38
        $this->setRequired(/** @scrutinizer ignore-type */ $testData->getInputItem('required'));
Loading history...
39
        $this->setAvailable($testData->getInputItem('available'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('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

39
        $this->setAvailable(/** @scrutinizer ignore-type */ $testData->getInputItem('available'));
Loading history...
40
        $this->setDependencies($testData->getInputItem('dependencies'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('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

40
        $this->setDependencies(/** @scrutinizer ignore-type */ $testData->getInputItem('dependencies'));
Loading history...
41
        $this->setType($testData->getInputItem('type'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('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

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