aboutsummaryrefslogtreecommitdiff
path: root/private/model/Connection.php
blob: 32cbf33cb9271ae393dbdeaaf758f27324d163fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

declare(strict_types=1);

namespace AugustOffensive\model;

/**
 * Model connection class for connecting to database via PDO.
 */
class Connection
{
    /** @var \PDO $_conn PDO connection to database. */
    private $_conn;
    /**
     * Initiates connection to PostGreSQL database.
     *
     * @return Connection
     */
    public function __construct ()
    {
        // Establish connection to db
        include 'creds.php';

        try {
	        $_conn = new \PDO(
                "pgsql: host=" . $cred->host .
                    (($cred->port !== '') ? ";port=" . $cred->port : '') .
                    ";dbname=" . $cred->dbName,
                $cred->login,
                $cred->password
            );
            // we destroy $cred as quickly as possible
            $cred = null;
        } catch (\PDOException $e) {
            // we destroy $cred as quickly as possible
            $cred = null;
            die(json_encode(array("Result-Type" => "Error", "Content" => array($e))));
        }
        return $this;
    }
}