Completed
Pull Request — master (#16)
by Bartko
786:06 queued 738:59
created

Pdo::executeInsertSQL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

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