Module   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 30
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeTest() 0 22 4
A getModule() 0 3 1
A getAvailable() 0 3 1
A initialize() 0 11 2
A getModuleName() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\monitor\TestCase;
6
7
use SimpleSAML\Module\monitor\State as State;
8
use SimpleSAML\Module\monitor\TestData as TestData;
9
use SimpleSAML\Module\monitor\TestResult as TestResult;
10
11
use function explode;
12
use function in_array;
13
use function is_null;
14
15
class Module extends \SimpleSAML\Module\monitor\TestCaseFactory
16
{
17
    /** @var array */
18
    private array $parsed = [];
19
20
    /** @var array */
21
    private array $available = [];
22
23
    /** @var string */
24
    private string $module;
25
26
27
    /**
28
     * @param \SimpleSAML\Module\monitor\TestData $testData
29
     *
30
     * @return void
31
     */
32
    protected function initialize(TestData $testData): void
33
    {
34
        $this->module = $testData->getInputItem('required');
35
        $available = $testData->getInputItem('available');
36
        if (!is_null($available)) {
37
            $this->available = $available;
38
        }
39
        $this->parsed = explode('|', $this->module);
40
41
        $this->setCategory($testData->getInputItem('type'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('type') can also be of type null; however, parameter $category of SimpleSAML\Module\monito...eFactory::setCategory() 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->setCategory(/** @scrutinizer ignore-type */ $testData->getInputItem('type'));
Loading history...
42
        parent::initialize($testData);
43
    }
44
45
46
    /**
47
     * @return void
48
     */
49
    public function invokeTest(): void
50
    {
51
        $testResult = new TestResult($this->getCategory(), $this->getModuleName());
52
53
        $state = State::ERROR;
54
        $available = $this->getAvailable();
55
56
        foreach ($this->parsed as $module) {
57
            if (in_array($module, $available)) {
58
                $state = State::OK;
59
                break 1;
60
            }
61
        }
62
63
        if ($state == State::OK) {
64
            $testResult->setMessage('Module loaded');
65
        } else {
66
            $testResult->setMessage('Module not loaded');
67
        }
68
69
        $testResult->setState($state);
70
        $this->setTestResult($testResult);
71
    }
72
73
74
    /**
75
     * @return array
76
     */
77
    private function getAvailable(): array
78
    {
79
        return $this->available;
80
    }
81
82
83
    /**
84
     * @return string
85
     */
86
    private function getModule(): string
87
    {
88
        return $this->module;
89
    }
90
91
92
    /**
93
     * @return string
94
     */
95
    public function getModuleName(): string
96
    {
97
        $available = $this->available;
98
99
        foreach ($this->parsed as $module) {
100
            if (in_array($module, $available)) {
101
                return $module;
102
            }
103
        }
104
105
        return $this->getModule();
106
    }
107
}
108