Passed
Push — master ( 745e3e...d48383 )
by Tim
01:22
created

Database::areModulesDependingOnDatabase()   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->areModulesDependingOnDatabase() === false) {
58
            $testResult = new TestResult('Database connection', '-');
59
            $testResult->setState(State::SKIPPED);
60
            $testResult->setMessage('Database currently not in use');
61
            $this->addTestResult($testResult);
62
            $this->setTestResult($testResult);
63
            return;
64
        } // We're using consent (TODO: but are we using consent+pdo??)
65
66
        $testData = new TestData(['dsn' => $this->dsn]);
67
        $connTest = new TestCase\Database\Connection($testData);
68
        $testResult = $connTest->getTestResult();
69
        $this->addTestResult($testResult);
70
        $this->setState($this->calculateState());
0 ignored issues
show
Bug introduced by
The method setState() does not exist on SimpleSAML\Module\monito...\Configuration\Database. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->/** @scrutinizer ignore-call */ 
71
               setState($this->calculateState());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    private function areModulesDependingOnDatabase()
77
    {
78
        foreach ($this->dependentModules as $module) {
79
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
}
86