OracleConnector   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.93%

Importance

Changes 0
Metric Value
dl 0
loc 197
ccs 51
cts 58
cp 0.8793
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 10 2
A getDsn() 0 15 2
A setPort() 0 6 2
A setServiceId() 0 8 2
A setTNS() 0 6 1
A setCharset() 0 8 2
A createConnection() 0 12 2
A parseConfig() 0 11 1
A setHost() 0 6 2
A setProtocol() 0 6 2
A checkMultipleHostDsn() 0 17 4
1
<?php
2
3
namespace Yajra\Oci8\Connectors;
4
5
use Illuminate\Database\Connectors\Connector;
6
use Illuminate\Database\Connectors\ConnectorInterface;
7
use PDO;
8
use Yajra\Pdo\Oci8;
9
10
class OracleConnector extends Connector implements ConnectorInterface
11
{
12
    /**
13
     * The default PDO connection options.
14
     *
15
     * @var array
16
     */
17
    protected $options = [
18
        PDO::ATTR_CASE         => PDO::CASE_LOWER,
19
        PDO::ATTR_ERRMODE      => PDO::ERRMODE_EXCEPTION,
20
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
21
    ];
22
23
    /**
24
     * Establish a database connection.
25
     *
26
     * @param array $config
27
     * @return PDO
28
     */
29 42
    public function connect(array $config)
30
    {
31 42
        $tns = ! empty($config['tns']) ? $config['tns'] : $this->getDsn($config);
32
33 42
        $options = $this->getOptions($config);
34
35 42
        $connection = $this->createConnection($tns, $config, $options);
36
37 42
        return $connection;
38
    }
39
40
    /**
41
     * Create a DSN string from a configuration.
42
     *
43
     * @param  array $config
44
     * @return string
45
     */
46 24
    protected function getDsn(array $config)
47
    {
48 24
        if (! empty($config['tns'])) {
49
            return $config['tns'];
50
        }
51
52
        // parse configuration
53 24
        $config = $this->parseConfig($config);
54
55
        // check multiple connections/host, comma delimiter
56 24
        $config = $this->checkMultipleHostDsn($config);
57
58
        // return generated tns
59 24
        return $config['tns'];
60
    }
61
62
    /**
63
     * Parse configurations.
64
     *
65
     * @param array $config
66
     * @return array
67
     */
68 24
    protected function parseConfig(array $config)
69
    {
70 24
        $config = $this->setHost($config);
71 24
        $config = $this->setPort($config);
72 24
        $config = $this->setProtocol($config);
73 24
        $config = $this->setServiceId($config);
74 24
        $config = $this->setTNS($config);
75 24
        $config = $this->setCharset($config);
76
77 24
        return $config;
78
    }
79
80
    /**
81
     * Set host from config.
82
     *
83
     * @param array $config
84
     * @return array
85
     */
86 24
    protected function setHost(array $config)
87
    {
88 24
        $config['host'] = isset($config['host']) ? $config['host'] : $config['hostname'];
89
90 24
        return $config;
91
    }
92
93
    /**
94
     * Set port from config.
95
     *
96
     * @param array $config
97
     * @return array
98
     */
99 24
    private function setPort(array $config)
100
    {
101 24
        $config['port'] = isset($config['port']) ? $config['port'] : '1521';
102
103 24
        return $config;
104
    }
105
106
    /**
107
     * Set protocol from config.
108
     *
109
     * @param array $config
110
     * @return array
111
     */
112 24
    private function setProtocol(array $config)
113
    {
114 24
        $config['protocol'] = isset($config['protocol']) ? $config['protocol'] : 'TCP';
115
116 24
        return $config;
117
    }
118
119
    /**
120
     * Set service id from config.
121
     *
122
     * @param array $config
123
     * @return array
124
     */
125 24
    protected function setServiceId(array $config)
126
    {
127 24
        $config['service'] = empty($config['service_name'])
128 24
            ? $service_param = 'SID = ' . $config['database']
129 24
            : $service_param = 'SERVICE_NAME = ' . $config['service_name'];
130
131 24
        return $config;
132
    }
133
134
    /**
135
     * Set tns from config.
136
     *
137
     * @param array $config
138
     * @return array
139
     */
140 24
    protected function setTNS(array $config)
141
    {
142 24
        $config['tns'] = "(DESCRIPTION = (ADDRESS = (PROTOCOL = {$config['protocol']})(HOST = {$config['host']})(PORT = {$config['port']})) (CONNECT_DATA =({$config['service']})))";
143
144 24
        return $config;
145
    }
146
147
    /**
148
     * Set charset from config.
149
     *
150
     * @param array $config
151
     * @return array
152
     */
153 24
    protected function setCharset(array $config)
154
    {
155 24
        if (! isset($config['charset'])) {
156 24
            $config['charset'] = 'AL32UTF8';
157 24
        }
158
159 24
        return $config;
160
    }
161
162
    /**
163
     * Set DSN host from config.
164
     *
165
     * @param array $config
166
     * @return array
167
     */
168 24
    protected function checkMultipleHostDsn(array $config)
169
    {
170 24
        $host = is_array($config['host']) ? $config['host'] : explode(',', $config['host']);
171
172 24
        $count = count($host);
173 24
        if ($count > 1) {
174 6
            $address = "";
175 6
            for ($i = 0; $i < $count; $i++) {
176 6
                $address .= '(ADDRESS = (PROTOCOL = ' . $config["protocol"] . ')(HOST = ' . trim($host[$i]) . ')(PORT = ' . $config['port'] . '))';
177 6
            }
178
179
            // create a tns with multiple address connection
180 6
            $config['tns'] = "(DESCRIPTION = {$address} (LOAD_BALANCE = yes) (FAILOVER = on) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = {$config['database']})))";
181 6
        }
182
183 24
        return $config;
184
    }
185
186
    /**
187
     * Create a new PDO connection.
188
     *
189
     * @param  string $tns
190
     * @param  array $config
191
     * @param  array $options
192
     * @return PDO
193
     */
194
    public function createConnection($tns, array $config, array $options)
195
    {
196
        // add fallback in case driver is not set, will use pdo instead
197
        if (! in_array($config['driver'], ['oci8', 'pdo-via-oci8', 'oracle'])) {
198
            return parent::createConnection($tns, $config, $options);
199
        }
200
201
        $config             = $this->setCharset($config);
202
        $options['charset'] = $config['charset'];
203
204
        return new Oci8($tns, $config['username'], $config['password'], $options);
205
    }
206
}
207