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

NestedTransactionDecorator::quoteIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet\Adapter;
6
7
class NestedTransactionDecorator implements AdapterInterface
8
{
9
    private $counter = 0;
10
    private $rollbackOnly = false;
11
    private $transactionWasOpenOutside = false;
12
13
    private $adapter;
14
15 77
    public function __construct(AdapterInterface $adapter)
16
    {
17 77
        $this->adapter = $adapter;
18 77
    }
19
20 73
    public function getAdapter(): AdapterInterface
21
    {
22 73
        return $this->adapter;
23
    }
24
25 39
    public function beginTransaction(): void
26
    {
27 39
        $a = $this->getAdapter();
28
29 39
        if ($a->canHandleNestedTransaction()) {
30 1
            $a->beginTransaction();
31
        } else {
32 38
            if (0 === $this->counter && $a->isInTransaction()) {
33 1
                $this->transactionWasOpenOutside = true;
34
            }
35
36 38
            if (0 === $this->counter && false === $this->transactionWasOpenOutside) {
37 38
                $a->beginTransaction();
38 38
                $this->counter = $this->counter + 1;
39
            } else {
40 5
                $this->counter = $this->counter + 1;
41
            }
42
        }
43 39
    }
44
45 22
    public function commitTransaction(): void
46
    {
47 22
        $a = $this->getAdapter();
48
49 22
        if ($a->canHandleNestedTransaction()) {
50 1
            $a->commitTransaction();
51
        } else {
52 21
            if ($this->rollbackOnly) {
53 1
                throw new \Exception('Cannot commit Transaction was marked as rollback only');
54
            }
55
56 20
            if (1 === $this->counter) {
57 20
                if (false === $this->transactionWasOpenOutside) {
58 20
                    $a->commitTransaction();
59
                } else {
60 1
                    $this->transactionWasOpenOutside = false;
61
                }
62 20
                $this->counter = 0;
63
            } else {
64 2
                $this->counter = $this->counter - 1;
65
            }
66
        }
67 21
    }
68
69 21
    public function rollbackTransaction(): void
70
    {
71 21
        $a = $this->getAdapter();
72
73 21
        if ($a->canHandleNestedTransaction()) {
74 1
            $a->rollbackTransaction();
75
        } else {
76 20
            if (1 === $this->counter) {
77 19
                if (false === $this->transactionWasOpenOutside) {
78 19
                    $a->rollbackTransaction();
79
                }
80 19
                $this->counter = 0;
81 19
                $this->rollbackOnly = false;
82 19
                $this->transactionWasOpenOutside = false;
83
            } else {
84 3
                $this->counter = $this->counter - 1;
85
            }
86
87 20
            if ($this->counter > 0) {
88 3
                $this->rollbackOnly = true;
89
            }
90
        }
91 21
    }
92
93
    public function isInTransaction(): bool
94
    {
95
        return $this->getAdapter()
96
                    ->isInTransaction();
97
    }
98
99 1
    public function canHandleNestedTransaction(): bool
100
    {
101 1
        return true;
102
    }
103
104 57
    public function quoteIdentifier(string $columnName): string
105
    {
106 57
        return $this->getAdapter()
107 57
                    ->quoteIdentifier($columnName);
108
    }
109
110 10
    public function executeInsertSQL(string $sql, array $params = array())
111
    {
112 10
        return $this->getAdapter()
113 10
                    ->executeInsertSQL($sql, $params);
114
    }
115
116 33
    public function executeSQL(string $sql, array $params = array()): void
117
    {
118 33
        $this->getAdapter()
119 33
             ->executeSQL($sql, $params);
120 33
    }
121
122 56
    public function executeSelectSQL(string $sql, array $params = array()): array
123
    {
124 56
        return $this->getAdapter()
125 56
                    ->executeSelectSQL($sql, $params);
126
    }
127
}
128