Completed
Push — master ( 5c5966...90cf17 )
by Marco
23s
created

AbstractMySQLDriver::convertException()   D

Complexity

Conditions 53
Paths 53

Size

Total Lines 78
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 66
CRAP Score 53

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 78
ccs 66
cts 66
cp 1
rs 4.9783
cc 53
eloc 66
nc 53
nop 2
crap 53

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver;
24
use Doctrine\DBAL\Exception;
25
use Doctrine\DBAL\Platforms\MySQL57Platform;
26
use Doctrine\DBAL\Platforms\MySqlPlatform;
27
use Doctrine\DBAL\Schema\MySqlSchemaManager;
28
use Doctrine\DBAL\VersionAwarePlatformDriver;
29
30
/**
31
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for MySQL based drivers.
32
 *
33
 * @author Steve Müller <[email protected]>
34
 * @link   www.doctrine-project.org
35
 * @since  2.5
36
 */
37
abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
38
{
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
43
     * @link http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
44
     */
45 4
    public function convertException($message, DriverException $exception)
46
    {
47 4
        switch ($exception->getErrorCode()) {
48 4
            case '1213':
49 4
                return new Exception\DeadlockException($message, $exception);
50 4
            case '1205':
51 4
                return new Exception\LockWaitTimeoutException($message, $exception);
52 4
            case '1050':
53 4
                return new Exception\TableExistsException($message, $exception);
54
55 4
            case '1051':
56 4
            case '1146':
57 4
                return new Exception\TableNotFoundException($message, $exception);
58
59 4
            case '1216':
60 4
            case '1217':
61 4
            case '1451':
62 4
            case '1452':
63 4
            case '1701':
64 4
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
65
66 4
            case '1062':
67 4
            case '1557':
68 4
            case '1569':
69 4
            case '1586':
70 4
                return new Exception\UniqueConstraintViolationException($message, $exception);
71
72 4
            case '1054':
73 4
            case '1166':
74 4
            case '1611':
75 4
                return new Exception\InvalidFieldNameException($message, $exception);
76
77 4
            case '1052':
78 4
            case '1060':
79 4
            case '1110':
80 4
                return new Exception\NonUniqueFieldNameException($message, $exception);
81
82 4
            case '1064':
83 4
            case '1149':
84 4
            case '1287':
85 4
            case '1341':
86 4
            case '1342':
87 4
            case '1343':
88 4
            case '1344':
89 4
            case '1382':
90 4
            case '1479':
91 4
            case '1541':
92 4
            case '1554':
93 4
            case '1626':
94 4
                return new Exception\SyntaxErrorException($message, $exception);
95
96 4
            case '1044':
97 4
            case '1045':
98 4
            case '1046':
99 4
            case '1049':
100 4
            case '1095':
101 4
            case '1142':
102 4
            case '1143':
103 4
            case '1227':
104 4
            case '1370':
105 4
            case '1429':
106 4
            case '2002':
107 4
            case '2005':
108 4
                return new Exception\ConnectionException($message, $exception);
109
110 4
            case '1048':
111 4
            case '1121':
112 4
            case '1138':
113 4
            case '1171':
114 4
            case '1252':
115 4
            case '1263':
116 4
            case '1364':
117 4
            case '1566':
118 4
                return new Exception\NotNullConstraintViolationException($message, $exception);
119
        }
120
121 4
        return new Exception\DriverException($message, $exception);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 6
    public function createDatabasePlatformForVersion($version)
128
    {
129 6
        if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) {
130 3
            throw DBALException::invalidPlatformVersionSpecified(
131 3
                $version,
132 3
                '<major_version>.<minor_version>.<patch_version>'
133
            );
134
        }
135
136 3
        if (false !== stripos($version, 'mariadb')) {
137 3
            return $this->getDatabasePlatform();
138
        }
139
140 3
        $majorVersion = $versionParts['major'];
141 3
        $minorVersion = isset($versionParts['minor']) ? $versionParts['minor'] : 0;
142 3
        $patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : null;
143
144 3
        if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) {
145 3
            $patchVersion = '9';
146
        }
147
148 3
        $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
149
150 3
        if (version_compare($version, '5.7.9', '>=')) {
151 3
            return new MySQL57Platform();
152
        }
153
154 3
        return $this->getDatabasePlatform();
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 4 View Code Duplication
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162 4
        $params = $conn->getParams();
163
164 4
        if (isset($params['dbname'])) {
165 4
            return $params['dbname'];
166
        }
167
168 4
        return $conn->query('SELECT DATABASE()')->fetchColumn();
0 ignored issues
show
Bug Compatibility introduced by
The expression $conn->query('SELECT DATABASE()')->fetchColumn(); of type string|boolean adds the type boolean to the return on line 168 which is incompatible with the return type declared by the interface Doctrine\DBAL\Driver::getDatabase of type string.
Loading history...
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 6
    public function getDatabasePlatform()
175
    {
176 6
        return new MySqlPlatform();
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 3
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
183
    {
184 3
        return new MySqlSchemaManager($conn);
185
    }
186
}
187