Database::pdo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace litemerafrukt\Database;
4
5
/**
6
 * Raw database connection and querier
7
 */
8
class Database
9
{
10
    private $pdo;
11
12
    /**
13
     * Connect to db
14
     */
15
    public function __construct($dns)
16
    {
17
        try {
18
            $this->pdo = new \PDO($dns);
19
            $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
20
        } catch (\Exception $e) {
21
            // Rethrow to hide connection details, through the original
22
            // exception to view all connection details.
23
            // throw $e;
24
            throw new \PDOException("Could not connect to database.");
25
        }
26
    }
27
28
    /**
29
     * Retrieve pdo. For testing.
30
     *
31
     * @return \PDO
32
     */
33 4
    public function pdo()
34
    {
35 4
        return $this->pdo;
36
    }
37
38
    /**
39
     * Execute query and return the PDOStatement
40
     *
41
     * @param string
42
     * @param array
43
     *
44
     * @return \PDOStatement
45
     */
46 8
    public function query($sql, $params = [])
47
    {
48
        try {
49 8
            $statement = $this->pdo->prepare($sql);
50 8
            $statement->execute($params);
51
        } catch (\PDOException $exception) {
52
            throw new \PDOException("Error in database query execution.");
53
        }
54
55 8
        return $statement;
56
    }
57
}
58