Completed
Push — master ( a98c98...92b268 )
by Bartko
02:00
created

Pdo::executeInsertSQL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.054
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\Adapter;
6
7
use StefanoTree\NestedSet\Options;
8
9
class Pdo implements AdapterInterface
10
{
11
    private $connection;
12
    private $options;
13
14
    /**
15
     * @param Options $options
16
     * @param \PDO    $connection
17
     */
18 128
    public function __construct(Options $options, \PDO $connection)
19
    {
20 128
        $this->connection = $connection;
21 128
        $this->options = $options;
22 128
    }
23
24
    /**
25
     * @return \PDO
26
     */
27 114
    private function getConnection(): \PDO
28
    {
29 114
        return $this->connection;
30
    }
31
32
    /**
33
     * @return Options
34
     */
35 15
    public function getOptions(): Options
36
    {
37 15
        return $this->options;
38
    }
39
40 35
    public function beginTransaction(): void
41
    {
42 35
        $this->getConnection()
43 35
             ->beginTransaction();
44 35
    }
45
46 18
    public function commitTransaction(): void
47
    {
48 18
        $this->getConnection()
49 18
             ->commit();
50 18
    }
51
52 18
    public function rollbackTransaction(): void
53
    {
54 18
        $this->getConnection()
55 18
             ->rollBack();
56 18
    }
57
58 35
    public function isInTransaction(): bool
59
    {
60 35
        return $this->getConnection()
61 35
                    ->inTransaction();
62
    }
63
64 31
    public function canHandleNestedTransaction(): bool
65
    {
66 31
        return false;
67
    }
68
69 108
    public function quoteIdentifier(string $columnName): string
70
    {
71 108
        return $columnName;
72
    }
73
74 15
    public function executeInsertSQL(string $sql, array $params = array())
75
    {
76 15
        $options = $this->getOptions();
77 15
        $this->executeSQL($sql, $params);
78
79 15
        if (array_key_exists($options->getIdColumnName(), $params)) {
80 2
            return $params[$options->getIdColumnName()];
81
        } else {
82 13
            if ('' != $options->getSequenceName()) {
83 13
                $lastGeneratedValue = $this->getConnection()
84 13
                                           ->lastInsertId($options->getSequenceName());
85
            } else {
86
                $lastGeneratedValue = $this->getConnection()
87
                                           ->lastInsertId();
88
            }
89
90 13
            return $lastGeneratedValue;
91
        }
92
    }
93
94 60
    public function executeSQL(string $sql, array $params = array()): void
95
    {
96 60
        $stm = $this->getConnection()
97 60
                    ->prepare($sql);
98 60
        $stm->execute($params);
99 60
    }
100
101 87
    public function executeSelectSQL(string $sql, array $params = array()): array
102
    {
103 87
        $stm = $this->getConnection()
104 87
                    ->prepare($sql);
105 87
        $stm->execute($params);
106
107 87
        return $stm->fetchAll(\PDO::FETCH_ASSOC);
108
    }
109
}
110