Passed
Branch monitor-2.5.x (32aff2)
by Tim
01:33
created

Connection::invokeTest()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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