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

Connection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A invokeTest() 0 23 4
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestCase\Database;
4
5
use SimpleSAML\Module\Monitor\State;
6
use SimpleSAML\Module\Monitor\TestData;
7
use SimpleSAML\Module\Monitor\TestResult;
8
9
final class Connection extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var \SimpleSAML\Database */
12
    private $db;
13
14
    /** @var string */
15
    private $dsn;
16
17
18
    /**
19
     * @param \SimpleSAML\Module\Monitor\TestData $testData
20
     *
21
     * @return void
22
     */
23
    protected function initialize(TestData $testData): void
24
    {
25
        $this->dsn = $testData->getInputItem('dsn');
26
        parent::initialize($testData);
27
    }
28
29
30
    /**
31
     * @return void
32
     */
33
    public function invokeTest(): void
34
    {
35
        try {
36
            $this->db = \SimpleSAML\Database::getInstance();
37
        } catch (\Exception $error) {
38
            // Fallthru
39
        }
40
41
        $testResult = new TestResult('Database connection', $this->dsn);
42
43
        if (isset($error)) {
44
            $testResult->setState(State::WARNING);
45
            $testResult->setMessage($error->getMessage());
46
        } elseif (isset($this->db)) {
47
            $testResult->setState(State::OK);
48
            $testResult->setMessage('Connection established');
49
            $testResult->addOutput($this->db, 'db');
50
        } else { // Shoud never happen
51
            $testResult->setState(State::WARNING);
52
            $testResult->setMessage("Something went wrong and we couldn't tell why");
53
        }
54
55
        $this->setTestResult($testResult);
56
    }
57
}
58