Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MysqlConnection
85.19% covered (warning)
85.19%
23 / 27
20.00% covered (danger)
20.00%
1 / 5
10.33
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 connect
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
5.01
 beginTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 rollBack
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 commit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\DataStorage\Database\Connection
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\DataStorage\Database\Connection;
16
17use phpOMS\DataStorage\Database\DatabaseStatus;
18use phpOMS\DataStorage\Database\DatabaseType;
19use phpOMS\DataStorage\Database\Query\Grammar\MysqlGrammar;
20use phpOMS\DataStorage\Database\Schema\Grammar\MysqlGrammar as MysqlSchemaGrammar;
21
22/**
23 * Database handler.
24 *
25 * Handles the database connection.
26 * Implementing wrapper functions for multiple databases is planned (far away).
27 *
28 * @package phpOMS\DataStorage\Database\Connection
29 * @license OMS License 2.0
30 * @link    https://jingga.app
31 * @since   1.0.0
32 */
33final class MysqlConnection extends ConnectionAbstract
34{
35    /**
36     * Object constructor.
37     *
38     * Creates the database object and overwrites all default values.
39     *
40     * @param array{db:string, host?:string, port?:int, login?:string, password?:string, database:string} $dbdata the basic database information for establishing a connection
41     *
42     * @since 1.0.0
43     */
44    public function __construct(array $dbdata)
45    {
46        $this->type          = DatabaseType::MYSQL;
47        $this->grammar       = new MysqlGrammar();
48        $this->schemaGrammar = new MysqlSchemaGrammar();
49
50        if (isset($dbdata['datetimeformat'])) {
51            $this->grammar->setDateTimeFormat($dbdata['datetimeformat']);
52            $this->schemaGrammar->setDateTimeFormat($dbdata['datetimeformat']);
53        }
54
55        $this->dbdata = $dbdata;
56    }
57
58    /**
59     * {@inheritdoc}
60     */
61    public function connect(array $dbdata = null) : void
62    {
63        if ($this->status === DatabaseStatus::OK) {
64            return;
65        }
66
67        $this->dbdata = $dbdata ?? $this->dbdata;
68
69        if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password'])
70            || !DatabaseType::isValidValue($this->dbdata['db'])
71        ) {
72            $this->status             = DatabaseStatus::FAILURE;
73            $this->dbdata['password'] = '****';
74
75            return;
76        }
77
78        $this->close();
79
80        try {
81            $this->con = new \PDO($this->dbdata['db'] . ':host=' . $this->dbdata['host'] . ':' . $this->dbdata['port'] . ';dbname=' . $this->dbdata['database'] . ';charset=utf8mb4', $this->dbdata['login'], $this->dbdata['password']);
82            $this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
83            $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
84
85            $this->status = DatabaseStatus::OK;
86        } catch (\PDOException $_) {
87            $this->con    = new NullPDO();
88            $this->status = DatabaseStatus::MISSING_DATABASE;
89        } finally {
90            $this->dbdata['password'] = '****';
91        }
92    }
93
94    /**
95     * {@inheritdoc}
96     */
97    public function beginTransaction() : void
98    {
99        $this->con->beginTransaction();
100    }
101
102    /**
103     * {@inheritdoc}
104     */
105    public function rollBack() : void
106    {
107        $this->con->rollBack();
108    }
109
110    /**
111     * {@inheritdoc}
112     */
113    public function commit() : void
114    {
115        $this->con->commit();
116    }
117}