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

Doctrine2DBAL::commitTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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