PDODbAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Agares\MicroORM;
5
6
final class PDODbAdapter implements DatabaseAdapterInterface
7
{
8
    /** @var \PDO */
9
    private $pdo;
10
11
    public function __construct(\PDO $pdo)
12
    {
13
        $this->pdo = $pdo;
14
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
    */
20
    public function executeCommand(string $command, array $parameters = array())
21
    {
22
        $this->executeParameterisedQuery($command, $parameters);
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function executeQuery(string $query, array $parameters = array()) : array
29
    {
30
        $statement = $this->executeParameterisedQuery($query, $parameters);
31
32
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
33
    }
34
35
    private function executeParameterisedQuery($query, array $parameters) : \PDOStatement
36
    {
37
        $statement = $this->pdo->prepare($query);
38
39
        foreach ($parameters as $name => $value) {
40
            $statement->bindValue($name, $value);
41
        }
42
43
        $statement->execute();
44
45
        return $statement;
46
    }
47
}