Database::areModulesDependingOnDatabase()   A
last analyzed

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