Passed
Push — master ( 5628bf...69a80f )
by Tim
01:19
created

Database::isDependent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 8
rs 10
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 if ($this->isDependent() === false) {
58
            // We're using consent (TODO: but are we using consent+pdo??)
59
            $testResult = new TestResult('Database connection', '-');
60
            $testResult->setState(State::SKIPPED);
61
            $testResult->setMessage('Database currently not in use');
62
            $this->addTestResult($testResult);
63
            $this->setTestResult($testResult);
64
            return;
65
        }
66
67
        // We are using the database-configuration in some way, so start testing it!
68
        $testData = new TestData(['dsn' => $this->dsn]);
69
        $connTest = new TestCase\Database\Connection($testData);
70
        $testResult = $connTest->getTestResult();
71
        $this->addTestResult($testResult);
72
        $testResult->setState(State::OK);
73
        $this->setTestResult($testResult);
74
    }
75
76
    private function isDependent()
77
    {
78
        foreach ($this->dependentModules as $module) {
79
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
}
86