Failed Conditions
Pull Request — master (#3033)
by Aleksey
13:48
created

Driver::_constructPdoDsn()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

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