Passed
Push — master ( 15d64f...e25937 )
by Jean Paul
01:30
created

PostgreSqlConnector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Database\Connections;
4
5
use Coco\SourceWatcher\Core\SourceWatcherException;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\DriverManager;
9
use Exception;
10
11
/**
12
 * Class PostgreSqlConnector
13
 * @package Coco\SourceWatcher\Core\Database\Connections
14
 *
15
 * Following the definition from https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-pgsql
16
 */
17
class PostgreSqlConnector extends Connector
18
{
19
    protected string $charset;
20
    protected string $defaultDatabaseName;
21
    protected string $sslMode;
22
    protected string $sslRootCert;
23
    protected string $sslCert;
24
    protected string $sslKey;
25
    protected string $sslCrl;
26
    protected string $applicationName;
27
28
    public function __construct ()
29
    {
30
        $this->driver = "pdo_pgsql";
31
    }
32
33
    public function connect () : Connection
34
    {
35
        try {
36
            return DriverManager::getConnection( $this->getConnectionParameters() );
37
        } catch ( DBALException $dbalException ) {
38
            throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() );
39
        } catch ( Exception $exception ) {
40
            throw new SourceWatcherException( "Something unexpected went wrong trying to get a connection: " . $exception->getMessage() );
41
        }
42
    }
43
44
    protected function getConnectionParameters () : array
45
    {
46
        $this->connectionParameters = array();
47
48
        $this->connectionParameters["driver"] = $this->driver;
49
        $this->connectionParameters["user"] = $this->user;
50
        $this->connectionParameters["password"] = $this->password;
51
        $this->connectionParameters["host"] = $this->host;
52
        $this->connectionParameters["port"] = $this->port;
53
        $this->connectionParameters["dbname"] = $this->dbName;
54
55
        $this->connectionParameters["charset"] = $this->charset;
56
        $this->connectionParameters["default_dbname"] = $this->defaultDatabaseName;
57
        $this->connectionParameters["sslmode"] = $this->sslMode;
58
        $this->connectionParameters["sslrootcert"] = $this->sslRootCert;
59
        $this->connectionParameters["sslcert"] = $this->sslCert;
60
        $this->connectionParameters["sslkey"] = $this->sslKey;
61
        $this->connectionParameters["sslcrl"] = $this->sslCrl;
62
        $this->connectionParameters["application_name"] = $this->applicationName;
63
64
        return $this->connectionParameters;
65
    }
66
}
67