Failed Conditions
Pull Request — master (#3260)
by Michael
61:30
created

PDOConnection::beginTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 5380
     * @param mixed[]|null $options
25
     *
26
     * @throws PDOException In case of an error.
27 5380
     */
28 5377
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
29 5377
    {
30 4715
        try {
31 4715
            $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
32
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33 5377
        } catch (\PDOException $exception) {
34
            throw new PDOException($exception);
35
        }
36
    }
37
38 5492
    /**
39
     * {@inheritdoc}
40
     */
41 5492
    public function exec(string $statement) : int
42 5445
    {
43 5445
        try {
44
            return $this->connection->exec($statement);
45
        } catch (\PDOException $exception) {
46
            throw new PDOException($exception);
47
        }
48
    }
49
50 5081
    /**
51
     * {@inheritdoc}
52 5081
     */
53
    public function getServerVersion()
54
    {
55
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
56
    }
57
58 5342
    /**
59
     * {@inheritdoc}
60
     */
61 5342
    public function prepare(string $sql) : Statement
62 5210
    {
63 5210
        try {
64
            return $this->createStatement(
65
                $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
        } catch (\PDOException $exception) {
68
            throw new PDOException($exception);
69
        }
70 5471
    }
71
72 5471
    /**
73
     * {@inheritdoc}
74
     */
75 5471
    public function query(string $sql) : ResultStatement
76 5308
    {
77
        try {
78 5308
            $stmt = $this->connection->query($sql);
79 5297
            assert($stmt instanceof \PDOStatement);
80 5297
81
            return $this->createStatement($stmt);
82
        } catch (\PDOException $exception) {
83
            throw new PDOException($exception);
84
        }
85
    }
86
87 4860
    /**
88
     * {@inheritdoc}
89 4860
     */
90
    public function quote(string $input) : string
91
    {
92
        return $this->connection->quote($input);
93
    }
94
95 521
    /**
96
     * {@inheritdoc}
97
     */
98 521
    public function lastInsertId($name = null)
99 171
    {
100
        try {
101
            if ($name === null) {
102 350
                return $this->connection->lastInsertId();
103
            }
104
105
            return $this->connection->lastInsertId($name);
106
        } catch (\PDOException $exception) {
107
            throw new PDOException($exception);
108
        }
109
    }
110
111 5082
    /**
112
     * {@inheritdoc}
113 5082
     */
114
    public function requiresQueryForServerVersion()
115
    {
116
        return false;
117
    }
118
119
    /**
120
     * Creates a wrapped statement
121
     */
122
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
123
    {
124
        return new PDOStatement($stmt);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function beginTransaction() : void
131
    {
132
        $this->connection->beginTransaction();
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function commit() : void
139
    {
140
        $this->connection->commit();
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function rollBack() : void
147
    {
148
        $this->connection->rollBack();
149
    }
150
151
    public function getWrappedConnection() : PDO
152
    {
153
        return $this->connection;
154
    }
155
}
156