Passed
Push — 2.7 ( bd5e19...0d26c7 )
by Sergei
14:54
created

Driver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 6.9%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 70
ccs 2
cts 29
cp 0.069
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A connect() 0 17 2
A buildDsn() 0 20 4
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\SQLAnywhere;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver\AbstractSQLAnywhereDriver;
24
use function array_keys;
25
use function array_map;
26
use function implode;
27
28
/**
29
 * A Doctrine DBAL driver for the SAP Sybase SQL Anywhere PHP extension.
30
 *
31
 * @author Steve Müller <[email protected]>
32
 * @link   www.doctrine-project.org
33
 * @since  2.5
34
 */
35
class Driver extends AbstractSQLAnywhereDriver
36
{
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @throws \Doctrine\DBAL\DBALException if there was a problem establishing the connection.
41
     */
42
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
43
    {
44
        try {
45
            return new SQLAnywhereConnection(
46
                $this->buildDsn(
47
                    $params['host'] ?? null,
48
                    $params['port'] ?? null,
49
                    $params['server'] ?? null,
50
                    $params['dbname'] ?? null,
51
                    $username,
52
                    $password,
53
                    $driverOptions
54
                ),
55
                $params['persistent'] ?? false
56
            );
57
        } catch (SQLAnywhereException $e) {
58
            throw DBALException::driverException($this, $e);
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function getName()
66
    {
67 1
        return 'sqlanywhere';
68
    }
69
70
    /**
71
     * Build the connection string for given connection parameters and driver options.
72
     *
73
     * @param string $host          Host address to connect to.
74
     * @param int    $port          Port to use for the connection (default to SQL Anywhere standard port 2638).
75
     * @param string $server        Database server name on the host to connect to.
76
     *                              SQL Anywhere allows multiple database server instances on the same host,
77
     *                              therefore specifying the server instance name to use is mandatory.
78
     * @param string $dbname        Name of the database on the server instance to connect to.
79
     * @param string $username      User name to use for connection authentication.
80
     * @param string $password      Password to use for connection authentication.
81
     * @param array  $driverOptions Additional parameters to use for the connection.
82
     *
83
     * @return string
84
     */
85
    private function buildDsn($host, $port, $server, $dbname, $username = null, $password = null, array $driverOptions = [])
86
    {
87
        $host = $host ?: 'localhost';
88
        $port = $port ?: 2638;
89
90
        if (! empty($server)) {
91
            $server = ';ServerName=' . $server;
92
        }
93
94
        return
95
            'HOST=' . $host . ':' . $port .
96
            $server .
97
            ';DBN=' . $dbname .
98
            ';UID=' . $username .
99
            ';PWD=' . $password .
100
            ';' . implode(
101
                ';',
102
                array_map(function ($key, $value) {
103
                    return $key . '=' . $value;
104
                }, array_keys($driverOptions), $driverOptions)
105
            );
106
    }
107
}
108