Completed
Push — master ( b97768...587712 )
by Bartko
31:36
created

NestedTransactionDecorator::commitTransaction()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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