Test Failed
Pull Request — develop (#340)
by Felipe
05:31
created

Connection::getDriver()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 34
rs 9.3554
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.0.0
5
 */
6
7
namespace PHPPgAdmin;
8
9
/**
10
 * @file
11
 * Class to represent a database connection
12
 *
13
 * Id: Connection.php,v 1.15 2008/02/18 21:42:47 ioguix Exp $
14
 */
15
class Connection
16
{
17
    use \PHPPgAdmin\Traits\HelperTrait;
18
19
    public $conn;
20
21
    public $platform = 'UNKNOWN';
22
23
    /**
24
     * @var string
25
     */
26
    public $driver;
27
28
    protected $container;
29
30
    protected $server_info;
31
32
    protected $version_dictionary = [
33
        '13' => 'Postgres13',
34
        '12' => 'Postgres12',
35
        '11' => 'Postgres11',
36
        '10' => 'Postgres10',
37
        '9.7' => 'Postgres96',
38
        '9.6' => 'Postgres96',
39
        '9.5' => 'Postgres95',
40
        '9.4' => 'Postgres94',
41
        '9.3' => 'Postgres93',
42
        '9.2' => 'Postgres92',
43
        '9.1' => 'Postgres91',
44
        '9.0' => 'Postgres90',
45
    ];
46
47
    /**
48
     * @var string
49
     */
50
    private $pgVersion;
0 ignored issues
show
Coding Style introduced by
Private member variable "pgVersion" must be prefixed with an underscore
Loading history...
51
52
    private $adodb_driver = 'postgres9';
0 ignored issues
show
Coding Style introduced by
Private member variable "adodb_driver" must be prefixed with an underscore
Loading history...
53
54
    // or pdo
55
    // The backend platform.  Set to UNKNOWN by default.
56
    private $_connection_result;
57
58
    /**
59
     * Creates a new connection.  Will actually make a database connection.
60
     *
61
     * @param array           $server_info
62
     * @param string          $database    database name
63
     * @param \Slim\Container $container
64
     * @param int             $fetchMode   Defaults to associative.  Override for different behaviour
65
     */
66
    public function __construct($server_info, $database, $container, $fetchMode = ADODB_FETCH_ASSOC)
67
    {
68
        $host = $server_info['host'];
69
        $port = $server_info['port'];
70
        $sslmode = $server_info['sslmode'];
71
        $user = $server_info['username'];
72
        $password = $server_info['password'];
73
74
        $this->server_info = $server_info;
75
76
        $this->container = $container;
77
78
        // ADODB_Postgres9 Approach
79
        //$driver='postgres9';
80
        $this->conn = \ADONewConnection($this->adodb_driver);
81
        $this->conn->setFetchMode($fetchMode);
82
83
        // PDO Approach
84
85
        /*try {
86
        $this->_connection_result = $this->conn->connect($pghost, $user, $password, $database);
87
        $this->prtrace(['_connection_result' => $this->_connection_result, 'conn' => $this->conn]);
88
        } catch (\PHPPgAdmin\ADOdbException $e) {
89
        $this->prtrace(['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
90
         */
91
        try {
92
            $this->_connection_result = 'pdo' === $this->adodb_driver ?
0 ignored issues
show
introduced by
The condition 'pdo' === $this->adodb_driver is always false.
Loading history...
93
                $this->getPDOConnection($host, $port, $sslmode, $database, $user, $password, $fetchMode) :
94
                $this->getPG9Connection($host, $port, $sslmode, $database, $user, $password, $fetchMode);
95
96
            //$this->prtrace($this->conn);
97
        } catch (\Exception $e) {
98
            //dump($dsnString, $this->adodb_driver);
99
            $this->prtrace($e->getMessage(), \array_slice($e->getTrace(), 0, 10));
100
        }
101
    }
102
103
    public function getConnectionResult()
104
    {
105
        return $this->_connection_result;
106
    }
107
108
    public function getVersion()
109
    {
110
        return $this->pgVersion;
111
    }
112
113
    /**
114
     * Gets the name of the correct database driver to use.  As a side effect,
115
     * sets the platform.
116
     *
117
     * @param string $description A description of the database and version (returns by reference)
118
     *
119
     * @return string The driver. e.g. Postgres96
120
     */
121
    public function getDriver(&$description)
122
    {
123
        if (!$this->conn->IsConnected()) {
124
            return null;
125
        }
126
        $serverInfo = $this->conn->ServerInfo();
127
        dump($serverInfo);
0 ignored issues
show
Bug introduced by
The call to dump() has too few arguments starting with str. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        /** @scrutinizer ignore-call */ 
128
        dump($serverInfo);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
128
        $this->pgVersion = $serverInfo['version'];
129
        $description = "PostgreSQL {$this->pgVersion}";
130
131
        $version_parts = \explode('.', $this->pgVersion);
132
133
        if ((int) (10 <= $version_parts[0])) {
134
            $major_version = $version_parts[0];
135
        } else {
136
            $major_version = \implode('.', [$version_parts[0], $version_parts[1]]);
137
        }
138
139
        // if major version is less than 9 return null, we don't support it
140
        if (9 > (float) $major_version) {
141
            $this->driver = null;
142
143
            return null;
144
        }
145
146
        $this->driver = 'Postgres';
147
        //$this->prtrace(['pg_version' => pg_version($this->conn->_connectionID), 'version' => $version, 'major_version' => $major_version]);
148
        // Detect version and choose appropriate database driver
149
        if (\array_key_exists($major_version, $this->version_dictionary)) {
150
            $this->driver = $this->version_dictionary[$major_version];
151
        }
152
153
        // If unknown version, then default to latest driver
154
        return $this->driver;
155
    }
156
157
    /**
158
     * Get the last error in the connection.
159
     *
160
     * @return string Error string
161
     */
162
    public function getLastError()
163
    {
164
        return $this->conn->ErrorMsg();
165
    }
166
167
    private function getPG9Connection(
168
        string $host,
169
        int $port,
170
        string $sslmode,
171
        ?string $database,
172
        ?string $user,
173
        ?string $password,
174
        int $fetchMode = \ADODB_FETCH_ASSOC
175
    ): \ADODB_postgres9 {
176
        $this->conn = ADONewConnection('postgres9');
177
        $this->conn->setFetchMode($fetchMode);
178
        // Ignore host if null
179
        if (null === $host || '' === $host) {
180
            if (null !== $port && '' !== $port) {
0 ignored issues
show
introduced by
The condition '' !== $port is always true.
Loading history...
181
                $pghost = ':' . $port;
182
            } else {
183
                $pghost = '';
184
            }
185
        } else {
186
            $pghost = "{$host}:{$port}";
187
        }
188
189
        // Add sslmode to $pghost as needed
190
        if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) {
191
            $pghost .= ':' . $sslmode;
192
        } elseif ('legacy' === $sslmode) {
193
            $pghost .= ' requiressl=1';
194
        }
195
196
        $this->conn->connect($pghost, $user, $password, $database);
197
198
        return $this->conn;
199
    }
200
201
    private function getPDOConnection(
202
        string $host,
203
        int $port,
204
        string $sslmode,
205
        ?string $database,
206
        ?string $user,
207
        ?string $password,
208
        int $fetchMode = \ADODB_FETCH_ASSOC
209
    ): \ADODB_pdo {
210
        $this->conn = ADONewConnection('pdo');
211
        $this->conn->setFetchMode($fetchMode);
212
        $dsnString = \sprintf('pgsql:host=%s;port=%d;dbname=%s;sslmode=%s;application_name=PHPPgAdmin6', $host, $port, $database, $sslmode);
213
        $this->conn->connect($dsnString, $user, $password);
214
215
        return $this->conn;
216
    }
217
}
218