Failed Conditions
Push — type-registry ( c9f3ba...324947 )
by Michael
22:26
created

Driver::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
4
5
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
6
use function is_int;
7
use function sprintf;
8
9
/**
10
 * The PDO-based Sqlsrv driver.
11
 */
12
class Driver extends AbstractSQLServerDriver
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
18
    {
19
        [$driverOptions, $connectionOptions] = $this->splitOptions($driverOptions);
20
21
        return new Connection(
22
            $this->_constructPdoDsn($params, $connectionOptions),
23
            $username,
24
            $password,
25
            $driverOptions
26
        );
27
    }
28
29
    /**
30
     * Constructs the Sqlsrv PDO DSN.
31
     *
32
     * @param mixed[]  $params
33
     * @param string[] $connectionOptions
34
     *
35
     * @return string The DSN.
36
     */
37
    private function _constructPdoDsn(array $params, array $connectionOptions)
38
    {
39
        $dsn = 'sqlsrv:server=';
40
41
        if (isset($params['host'])) {
42
            $dsn .= $params['host'];
43
        }
44
45
        if (isset($params['port']) && ! empty($params['port'])) {
46
            $dsn .= ',' . $params['port'];
47
        }
48
49
        if (isset($params['dbname'])) {
50
            $connectionOptions['Database'] = $params['dbname'];
51
        }
52
53
        if (isset($params['MultipleActiveResultSets'])) {
54
            $connectionOptions['MultipleActiveResultSets'] = $params['MultipleActiveResultSets'] ? 'true' : 'false';
55
        }
56
57
        return $dsn . $this->getConnectionOptionsDsn($connectionOptions);
58
    }
59
60
    /**
61
     * Separates a connection options from a driver options
62
     *
63
     * @param int[]|string[] $options
64
     *
65
     * @return int[][]|string[][]
66
     */
67
    private function splitOptions(array $options) : array
68
    {
69
        $driverOptions     = [];
70
        $connectionOptions = [];
71
72
        foreach ($options as $optionKey => $optionValue) {
73
            if (is_int($optionKey)) {
74
                $driverOptions[$optionKey] = $optionValue;
75
            } else {
76
                $connectionOptions[$optionKey] = $optionValue;
77
            }
78
        }
79
80
        return [$driverOptions, $connectionOptions];
81
    }
82
83
    /**
84
     * Converts a connection options array to the DSN
85
     *
86
     * @param string[] $connectionOptions
87
     */
88
    private function getConnectionOptionsDsn(array $connectionOptions) : string
89
    {
90
        $connectionOptionsDsn = '';
91
92
        foreach ($connectionOptions as $paramName => $paramValue) {
93
            $connectionOptionsDsn .= sprintf(';%s=%s', $paramName, $paramValue);
94
        }
95
96
        return $connectionOptionsDsn;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 21
    public function getName()
103
    {
104 21
        return 'pdo_sqlsrv';
105
    }
106
}
107