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

Module   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeTest() 0 22 4
A getAvailable() 0 3 1
A getModuleName() 0 11 3
A getModule() 0 3 1
A initialize() 0 11 2
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestCase;
4
5
use SimpleSAML\Module\Monitor\State as State;
6
use SimpleSAML\Module\Monitor\TestData as TestData;
7
use SimpleSAML\Module\Monitor\TestResult as TestResult;
8
9
class Module extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var array */
12
    private $parsed = [];
13
14
    /** @var array */
15
    private $available = [];
16
17
    /** @var string */
18
    private $module;
19
20
21
    /**
22
     * @param \SimpleSAML\Module\Monitor\TestData $testData
23
     *
24
     * @return void
25
     */
26
    protected function initialize(TestData $testData): void
27
    {
28
        $this->module = $testData->getInputItem('required');
29
        $available = $testData->getInputItem('available');
30
        if (!is_null($available)) {
31
            $this->available = $available;
32
        }
33
        $this->parsed = explode('|', $this->module);
34
35
        $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

35
        $this->setCategory(/** @scrutinizer ignore-type */ $testData->getInputItem('type'));
Loading history...
36
        parent::initialize($testData);
37
    }
38
39
40
    /**
41
     * @return void
42
     */
43
    public function invokeTest(): void
44
    {
45
        $testResult = new TestResult($this->getCategory(), $this->getModuleName());
46
47
        $state = State::ERROR;
48
        $available = $this->getAvailable();
49
50
        foreach ($this->parsed as $module) {
51
            if (in_array($module, $available)) {
52
                $state = State::OK;
53
                break 1;
54
            }
55
        }
56
57
        if ($state == State::OK) {
58
            $testResult->setMessage('Module loaded');
59
        } else {
60
            $testResult->setMessage('Module not loaded');
61
        }
62
63
        $testResult->setState($state);
64
        $this->setTestResult($testResult);
65
    }
66
67
68
    /**
69
     * @return array
70
     */
71
    private function getAvailable(): array
72
    {
73
        return $this->available;
74
    }
75
76
77
    /**
78
     * @return string
79
     */
80
    private function getModule(): string
81
    {
82
        return $this->module;
83
    }
84
85
86
    /**
87
     * @return string
88
     */
89
    public function getModuleName(): string
90
    {
91
        $available = $this->available;
92
93
        foreach ($this->parsed as $module) {
94
            if (in_array($module, $available)) {
95
                return $module;
96
            }
97
        }
98
99
        return $this->getModule();
100
    }
101
}
102