Completed
Pull Request — master (#3033)
by Aleksey
63:27
created

Driver::getConnectionOptionsDsn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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\PDOSqlsrv;
21
22
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
23
use function sprintf;
24
use function is_int;
25
26
/**
27
 * The PDO-based Sqlsrv driver.
28
 *
29
 * @since 2.0
30
 */
31
class Driver extends AbstractSQLServerDriver
32
{
33
    /**
34 27
     * {@inheritdoc}
35
     */
36 27
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
37 27
    {
38 27
        $splitedOptions = $this->splitOptions($driverOptions);
39 27
40 27
        return new Connection(
41
            $this->_constructPdoDsn($params, $splitedOptions['connectionOptions']),
42
            $username,
43
            $password,
44
            $splitedOptions['driverOptions']
45
        );
46
    }
47
48
    /**
49
     * Constructs the Sqlsrv PDO DSN.
50
     *
51 27
     * @param array $params
52
     * @param string[] $connectionOptions
53 27
     *
54
     * @return string The DSN.
55 27
     */
56 27
    private function _constructPdoDsn(array $params, array $connectionOptions)
57
    {
58
        $dsn = 'sqlsrv:server=';
59 27
60 27
        if (isset($params['host'])) {
61
            $dsn .= $params['host'];
62
        }
63 27
64 27
        if (isset($params['port']) && !empty($params['port'])) {
65
            $dsn .= ',' . $params['port'];
66
        }
67 27
68
        if (isset($params['dbname'])) {
69
            $dsn .= ';Database=' . $params['dbname'];
70
        }
71 27
72
        if (! empty($connectionOptions)) {
73
            $dsn .= $this->getConnectionOptionsDsn($connectionOptions);
74
        }
75
76
        return $dsn;
77 22
    }
78
79 22
    /**
80
     * Separates a connection options from a driver options
81
     *
82
     * @param array $options
83
     * @return array
84
     */
85
    private function splitOptions(array $options)
86
    {
87
        $driverOptions     = [];
88
        $connectionOptions = [];
89
90
        foreach ($options as $optionKey => $optionValue) {
91
            if (is_int($optionKey)) {
92
                $driverOptions[$optionKey] = $optionValue;
93
            } else {
94
                $connectionOptions[$optionKey] = $optionValue;
95
            }
96
        }
97
98
        return [
99
            'driverOptions' => $driverOptions,
100
            'connectionOptions' => $connectionOptions,
101
        ];
102
    }
103
104
    /**
105
     * Converts a connection options array to the DSN
106
     *
107
     * @param string[] $connectionOptions
108
     * @return string
109
     */
110
    private function getConnectionOptionsDsn(array $connectionOptions)
111
    {
112
        $connectionOptionsDsn = '';
113
114
        foreach ($connectionOptions as $paramName => $paramValue) {
115
            $connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue);
116
        }
117
118
        return $connectionOptionsDsn;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getName()
125
    {
126
        return 'pdo_sqlsrv';
127
    }
128
}
129