Pgsql   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 37
dl 0
loc 70
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A genDsn() 0 14 3
1
<?php
2
3
namespace DrMVC\Database\Drivers;
4
5
/**
6
 * Wrapper of PDO for work with PgSQL databases
7
 *
8
 * @package DrMVC\Database\Drivers
9
 * @since   3.0
10
 */
11
class Pgsql extends SQL
12
{
13
    const DEFAULT_HOST = '127.0.0.1';
14
    const DEFAULT_PORT = '5432';
15
16
    /**
17
     * @link https://secure.php.net/manual/en/ref.pdo-pgsql.connection.php
18
     * @link https://www.postgresql.org/docs/8.3/static/libpq-connect.html
19
     *
20
     * The PDO_PGSQL Data Source Name (DSN) is composed of the following elements:
21
     */
22
    const AVAILABLE_OPTIONS = [
23
        'host',
24
        'hostaddr',
25
        'port',
26
        'dbname',
27
28
        /**
29
         * These two parameters below is not needed, because we can put user/pass
30
         * as parameters of PDO class, you can read more by link
31
         * @link https://secure.php.net/manual/en/pdo.construct.php
32
         */
33
        //'user',
34
        //'password',
35
36
        'passfile',
37
        'connect_timeout',
38
        'client_encoding',
39
        'options',
40
        'application_name',
41
        'fallback_application_name',
42
        'keepalives',
43
        'keepalives_idle',
44
        'keepalives_interval',
45
        'keepalives_count',
46
        'tty',
47
        'sslmode',
48
        'requiressl',
49
        'sslcompression',
50
        'sslcert',
51
        'sslkey',
52
        'sslrootcert',
53
        'sslcrl',
54
        'requirepeer',
55
        'krbsrvname',
56
        'gsslib',
57
        'service',
58
        'target_session_attrs'
59
    ];
60
61
    /**
62
     * Generate DSN by parameters in config
63
     *
64
     * @param   array $config
65
     * @return  string
66
     */
67
    public function genDsn($config): string
68
    {
69
        // Parse config
70
        $dsn = '';
71
        foreach ($config as $key => $value) {
72
            if (\in_array($key, self::AVAILABLE_OPTIONS, false)) {
73
                $dsn .= "$key=$value;";
74
            }
75
        }
76
77
        // Get driver of connection
78
        $driver = strtolower($config['driver']);
79
80
        return "$driver:$dsn";
81
    }
82
}
83