Connection::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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