Passed
Push — master ( 4a5c66...5628bf )
by Tim
01:23
created

Database::invokeTest()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 10
nop 0
dl 0
loc 31
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\Configuration;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
use \SimpleSAML\Module\monitor\TestResult as TestResult;
9
use \SimpleSAML\Module\monitor\State as State;
10
use \SimpleSAML\Utils as Utils;
11
12
final class Database extends \SimpleSAML\Module\monitor\TestSuiteFactory
13
{
14
    /**
15
     * @var string|null
16
     */
17
    private $store = null;
18
19
    /**
20
     * @var array
21
     */
22
    private $metadataSources = [];
23
24
    /**
25
     * @var array
26
     */
27
    private $dependentModules = ['consent'];
28
29
    /**
30
     * @var string
31
     */
32
    private $dsn = 'undefined';
33
34
    /**
35
     * @param TestConfiguration $configuration
36
     */
37
    public function __construct($configuration)
38
    {
39
        $globalConfig = $configuration->getGlobalConfig();
40
        $this->store = $globalConfig->getString('store.type', 'phpsession');
41
        $this->dsn = $globalConfig->getString('database.dsn');
42
        $this->metadataSources = $globalConfig->getArray('metadata.sources', array());
43
44
        $this->setCategory('Configuration');
45
        parent::__construct($configuration);
46
    }
47
48
    /**
49
     * @return void
50
     */
51
    public function invokeTest()
52
    {
53
        if ($this->store === 'sql') {
54
            // We use a database for session-storage
55
        } else if (in_array(array('type' => 'pdo'), $this->metadataSources, true)) {
56
            // We use a database for metadata-storage
57
        } else {
58
            $dependent = false;
59
            foreach ($this->dependentModules as $module) {
60
                if (\SimpleSAML\Module::isModuleEnabled($module)) {
61
                    $dependent = true;
62
                    break 1;
63
                }
64
            }
65
            if ($dependent === false) {
66
                $testResult = new TestResult('Database connection', '-');
67
                $testResult->setState(State::SKIPPED);
68
                $testResult->setMessage('Database currently not in use');
69
                $this->addTestResult($testResult);
70
                $this->setTestResult($testResult);
71
                return;
72
            } // else we're using consent (TODO: are we using consent+pdo??)
73
        }
74
75
        // We are using the database-configuration in some way, so start testing it!
76
        $testData = new TestData(['dsn' => $this->dsn]);
77
        $connTest = new TestCase\Database\Connection($testData);
78
        $testResult = $connTest->getTestResult();
79
        $this->addTestResult($testResult);
80
        $testResult->setState(State::OK);
81
        $this->setTestResult($testResult);
82
    }
83
}
84