Connection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A unsetSqlSafeUpdates() 0 5 1
A setSqlSafeUpdates() 0 5 1
A parentBeginTransaction() 0 3 1
A __construct() 0 14 1
A beginTransaction() 0 9 3
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\Connection;
4
5
class Connection extends \PDO implements ConnectionInterface
6
{
7
    /**
8
     * Ignoring for code coverage, as this requires to have a real connection to be tested.
9
     * @codeCoverageIgnore
10
     *
11
     * @param string $dsn
12
     * @param string $username
13
     * @param string $passwd
14
     * @param array $options
15
     */
16
    public function __construct(string $dsn, string $username, string $passwd, array $options = [])
17
    {
18
        $options = \array_merge(
19
            $options,
20
            [
21
                static::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8mb4"',
22
                static::ATTR_PERSISTENT => true,
23
            ]
24
        );
25
26
        parent::__construct($dsn, $username, $passwd, $options);
27
28
        $this->setAttribute(static::ATTR_ERRMODE, static::ERRMODE_EXCEPTION);
29
        $this->setAttribute(static::ATTR_EMULATE_PREPARES, false);
30
    }
31
32
    /**
33
     * @return $this
34
     * @throws ConnectionPDOException
35
     */
36
    public function beginTransaction(): Connection
37
    {
38
        if (false === $this->inTransaction()) {
39
            if (false === $this->parentBeginTransaction()) {
40
                throw new ConnectionPDOException('Cannot begin transaction!');
41
            }
42
        }
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return $this
49
     */
50
    public function setSqlSafeUpdates(): Connection
51
    {
52
        $this->exec('SET SESSION SQL_SAFE_UPDATES = 1;');
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return $this
59
     */
60
    public function unsetSqlSafeUpdates(): Connection
61
    {
62
        $this->exec('SET SESSION SQL_SAFE_UPDATES = 0;');
63
64
        return $this;
65
    }
66
67
    /**
68
     * Ignoring for code coverage, as this requires to have a real connection to be tested and it is not possible
69
     * to mock parent method call.
70
     * @codeCoverageIgnore
71
     *
72
     * @return bool
73
     */
74
    protected function parentBeginTransaction(): bool
75
    {
76
        return parent::beginTransaction();
77
    }
78
}
79