summaryrefslogtreecommitdiffstats
path: root/library/X509/Common/Database.php
blob: d6eb3e18d8f23e79f886bb57680b4b4a83795577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

/* Icinga Web 2 X.509 Module | (c) 2022 Icinga GmbH | GPLv2 */

namespace Icinga\Module\X509\Common;

use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use ipl\Sql;
use PDO;

final class Database
{
    /** @var Sql\Connection Database connection */
    private static $instance;

    private function __construct()
    {
    }

    /**
     * Get the database connection
     *
     * @return Sql\Connection
     */
    public static function get(): Sql\Connection
    {
        if (self::$instance === null) {
            self::$instance = self::getDb();
        }

        return self::$instance;
    }

    /**
     * Get the connection to the X.509 database
     *
     * @return  Sql\Connection
     */
    private static function getDb(): Sql\Connection
    {
        $config = new Sql\Config(ResourceFactory::getResourceConfig(
            Config::module('x509')->get('backend', 'resource', 'x509')
        ));

        $options = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ];
        if ($config->db === 'mysql') {
            $options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE"
                . ",NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
        }

        $config->options = $options;

        return new Sql\Connection($config);
    }
}