Failed Conditions
Push — master ( 01c22b...e42c1f )
by Marco
79:13 queued 10s
created

Driver::connect()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.392

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 37
ccs 12
cts 15
cp 0.8
rs 8.8333
c 0
b 0
f 0
cc 7
nc 22
nop 4
crap 7.392
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOPgSql;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
9
use Doctrine\DBAL\Driver\Connection;
10
use Doctrine\DBAL\Driver\PDOConnection;
11
use Doctrine\DBAL\Driver\PDOException;
12
use PDO;
13
use function defined;
14
15
/**
16
 * Driver that connects through pdo_pgsql.
17
 */
18
class Driver extends AbstractPostgreSQLDriver
19
{
20 287
    /**
21
     * {@inheritdoc}
22
     */
23 287
    public function connect(
24 287
        array $params,
25
        string $username = '',
26
        string $password = '',
27
        array $driverOptions = []
28
    ) : Connection {
29
        if (! empty($params['persistent'])) {
30 287
            $driverOptions[PDO::ATTR_PERSISTENT] = true;
31 287
        }
32 287
33
        try {
34
            $connection = new PDOConnection(
35 287
                $this->_constructPdoDsn($params),
36
                $username,
37
                $password,
38
                $driverOptions
39
            );
40
41
            if (defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
42 287
                && (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
0 ignored issues
show
Bug introduced by
The constant PDO::PGSQL_ATTR_DISABLE_PREPARES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43 91
                    || $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
44
                )
45
            ) {
46 287
                $connection->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
47 161
            }
48 161
49
            /* defining client_encoding via SET NAMES to avoid inconsistent DSN support
50
             * - the 'client_encoding' connection param only works with postgres >= 9.1
51
             * - passing client_encoding via the 'options' param breaks pgbouncer support
52
             */
53
            if (isset($params['charset'])) {
54
                $connection->exec('SET NAMES \'' . $params['charset'] . '\'');
55
            }
56
57
            return $connection;
58
        } catch (PDOException $e) {
59 287
            throw DBALException::driverException($this, $e);
60
        }
61 287
    }
62
63 287
    /**
64 287
     * Constructs the Postgres PDO DSN.
65
     *
66
     * @param mixed[] $params
67 287
     *
68 287
     * @return string The DSN.
69
     */
70
    private function _constructPdoDsn(array $params) : string
71 287
    {
72 287
        $dsn = 'pgsql:';
73 287
74 126
        if (isset($params['host']) && $params['host'] !== '') {
75
            $dsn .= 'host=' . $params['host'] . ';';
76
        }
77
78
        if (isset($params['port']) && $params['port'] !== '') {
79 287
            $dsn .= 'port=' . $params['port'] . ';';
80
        }
81
82 287
        if (isset($params['dbname'])) {
83
            $dsn .= 'dbname=' . $params['dbname'] . ';';
84
        } elseif (isset($params['default_dbname'])) {
85
            $dsn .= 'dbname=' . $params['default_dbname'] . ';';
86 287
        } else {
87
            // Used for temporary connections to allow operations like dropping the database currently connected to.
88
            // Connecting without an explicit database does not work, therefore "postgres" database is used
89
            // as it is mostly present in every server setup.
90 287
            $dsn .= 'dbname=postgres;';
91
        }
92
93
        if (isset($params['sslmode'])) {
94 287
            $dsn .= 'sslmode=' . $params['sslmode'] . ';';
95
        }
96
97
        if (isset($params['sslrootcert'])) {
98 287
            $dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
99
        }
100
101
        if (isset($params['sslcert'])) {
102 287
            $dsn .= 'sslcert=' . $params['sslcert'] . ';';
103 112
        }
104
105
        if (isset($params['sslkey'])) {
106 287
            $dsn .= 'sslkey=' . $params['sslkey'] . ';';
107
        }
108
109
        if (isset($params['sslcrl'])) {
110
            $dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
111
        }
112
113
        if (isset($params['application_name'])) {
114 307
            $dsn .= 'application_name=' . $params['application_name'] . ';';
115
        }
116 307
117
        return $dsn;
118
    }
119
}
120