Completed
Push — master ( af81c1...f3339a )
by Sergei
21s queued 15s
created

AbstractOracleDriver::convertException()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 16

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 27
cts 27
cp 1
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 26
nc 16
nop 2
crap 16

How to fix   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\Driver;
23
use Doctrine\DBAL\Exception;
24
use Doctrine\DBAL\Platforms\OraclePlatform;
25
use Doctrine\DBAL\Schema\OracleSchemaManager;
26
27
/**
28
 * Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for Oracle based drivers.
29
 *
30
 * @author Steve Müller <[email protected]>
31
 * @link   www.doctrine-project.org
32
 * @since  2.5
33
 */
34
abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39 193
    public function convertException($message, DriverException $exception)
40
    {
41 193
        switch ($exception->getErrorCode()) {
42 193
            case '1':
43 191
            case '2299':
44 191
            case '38911':
45 56
                return new Exception\UniqueConstraintViolationException($message, $exception);
46
47 191
            case '904':
48 55
                return new Exception\InvalidFieldNameException($message, $exception);
49
50 190
            case '918':
51 189
            case '960':
52 55
                return new Exception\NonUniqueFieldNameException($message, $exception);
53
54 189
            case '923':
55 55
                return new Exception\SyntaxErrorException($message, $exception);
56
57 188
            case '942':
58 111
                return new Exception\TableNotFoundException($message, $exception);
59
60 187
            case '955':
61 93
                return new Exception\TableExistsException($message, $exception);
62
63 149
            case '1017':
64 147
            case '12545':
65 57
                return new Exception\ConnectionException($message, $exception);
66
67 146
            case '1400':
68 55
                return new Exception\NotNullConstraintViolationException($message, $exception);
69
70 145
            case '2266':
71 145
            case '2291':
72 145
            case '2292':
73 58
                return new Exception\ForeignKeyConstraintViolationException($message, $exception);
74
        }
75
76 145
        return new Exception\DriverException($message, $exception);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 153
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
83
    {
84 153
        $params = $conn->getParams();
85
86 153
        return $params['user'];
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 80
    public function getDatabasePlatform()
93
    {
94 80
        return new OraclePlatform();
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 64
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
101
    {
102 64
        return new OracleSchemaManager($conn);
103
    }
104
105
    /**
106
     * Returns an appropriate Easy Connect String for the given parameters.
107
     *
108
     * @param array $params The connection parameters to return the Easy Connect STring for.
109
     *
110
     * @return string
111
     *
112
     * @link https://docs.oracle.com/database/121/NETAG/naming.htm
113
     */
114 78
    protected function getEasyConnectString(array $params)
115
    {
116 78
        if ( ! empty($params['connectstring'])) {
117
            return $params['connectstring'];
118
        }
119
120 78
        if ( ! empty($params['host'])) {
121 78
            if ( ! isset($params['port'])) {
122
                $params['port'] = 1521;
123
            }
124
125 78
            $serviceName = $params['dbname'];
126
127 78
            if ( ! empty($params['servicename'])) {
128
                $serviceName = $params['servicename'];
129
            }
130
131 78
            $service = 'SID=' . $serviceName;
132 78
            $pooled  = '';
133 78
            $instance = '';
134
135 78
            if (isset($params['service']) && $params['service'] == true) {
136
                $service = 'SERVICE_NAME=' . $serviceName;
137
            }
138
139 78
            if (isset($params['instancename']) && ! empty($params['instancename'])) {
140
                $instance = '(INSTANCE_NAME = ' . $params['instancename'] . ')';
141
            }
142
143 78
            if (isset($params['pooled']) && $params['pooled'] == true) {
144
                $pooled = '(SERVER=POOLED)';
145
            }
146
147
            return '(DESCRIPTION=' .
148 78
                     '(ADDRESS=(PROTOCOL=TCP)(HOST=' . $params['host'] . ')(PORT=' . $params['port'] . '))' .
149 78
                     '(CONNECT_DATA=(' . $service . ')' . $instance . $pooled . '))';
150
151
        }
152
153
        return $params['dbname'] ?? '';
154
    }
155
}
156