Passed
Branch master (4b23d6)
by Tim
04:40
created

ModuleSet::findMissingDependencies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
        $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

33
        $this->setRequired(/** @scrutinizer ignore-type */ $testData->getInputItem('required'));
Loading history...
34
        $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

34
        $this->setAvailable(/** @scrutinizer ignore-type */ $testData->getInputItem('available'));
Loading history...
35
        $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

35
        $this->setDependencies(/** @scrutinizer ignore-type */ $testData->getInputItem('dependencies'));
Loading history...
36
        $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

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