Passed
Push — drop-deprecated ( db0b1f )
by Michael
27:00
created

PDOConnection   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 19
eloc 35
dl 0
loc 139
ccs 44
cts 46
cp 0.9565
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresQueryForServerVersion() 0 3 1
A query() 0 9 2
A prepare() 0 8 2
A __construct() 0 7 2
A beginTransaction() 0 3 1
A commit() 0 3 1
A getServerVersion() 0 3 1
A rollBack() 0 3 1
A lastInsertId() 0 10 3
A createStatement() 0 3 1
A exec() 0 6 2
A quote() 0 3 1
A getWrappedConnection() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver;
6
7
use PDO;
8
use function assert;
9
10
/**
11
 * PDO implementation of the Connection interface.
12
 *
13
 * Used by all PDO-based drivers.
14
 */
15
class PDOConnection implements Connection, ServerInfoAwareConnection
16
{
17
    /** @var PDO */
18
    private $connection;
19
20
    /**
21
     * @param string       $dsn
22
     * @param string|null  $user
23
     * @param string|null  $password
24
     * @param mixed[]|null $options
25
     *
26
     * @throws PDOException In case of an error.
27
     */
28 5012
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
29
    {
30
        try {
31 5012
            $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
32 5012
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33 4392
        } catch (\PDOException $exception) {
34 4392
            throw new PDOException($exception);
35
        }
36 5012
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5012
    public function exec(string $statement) : int
42
    {
43
        try {
44 5012
            return $this->connection->exec($statement);
45 5012
        } catch (\PDOException $exception) {
46 5012
            throw new PDOException($exception);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4514
    public function getServerVersion()
54
    {
55 4514
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 4916
    public function prepare(string $sql) : Statement
62
    {
63
        try {
64 4916
            return $this->createStatement(
65 4916
                $this->connection->prepare($sql)
0 ignored issues
show
Bug introduced by
It seems like $this->connection->prepare($sql) can also be of type boolean; however, parameter $stmt of Doctrine\DBAL\Driver\PDO...tion::createStatement() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

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

65
                /** @scrutinizer ignore-type */ $this->connection->prepare($sql)
Loading history...
66
            );
67 4916
        } catch (\PDOException $exception) {
68 4916
            throw new PDOException($exception);
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4988
    public function query(string $sql) : ResultStatement
76
    {
77
        try {
78 4988
            $stmt = $this->connection->query($sql);
79 4852
            assert($stmt instanceof \PDOStatement);
80
81 4852
            return $this->createStatement($stmt);
82 4988
        } catch (\PDOException $exception) {
83 4988
            throw new PDOException($exception);
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 4618
    public function quote(string $input) : string
91
    {
92 4618
        return $this->connection->quote($input);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 454
    public function lastInsertId($name = null)
99
    {
100
        try {
101 454
            if ($name === null) {
102 154
                return $this->connection->lastInsertId();
103
            }
104
105 300
            return $this->connection->lastInsertId($name);
106
        } catch (\PDOException $exception) {
107
            throw new PDOException($exception);
108
        }
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 4684
    public function requiresQueryForServerVersion()
115
    {
116 4684
        return false;
117
    }
118
119
    /**
120
     * Creates a wrapped statement
121
     */
122 4589
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124 4589
        return new PDOStatement($stmt);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 4726
    public function beginTransaction() : void
131
    {
132 4726
        $this->connection->beginTransaction();
133 4726
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138 4642
    public function commit() : void
139
    {
140 4642
        $this->connection->commit();
141 4642
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146 4672
    public function rollBack() : void
147
    {
148 4672
        $this->connection->rollBack();
149 4672
    }
150
151 5012
    public function getWrappedConnection() : PDO
152
    {
153 5012
        return $this->connection;
154
    }
155
}
156