MultipleTransactionManager::rollback()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace RemiSan\TransactionManager;
4
5
use RemiSan\TransactionManager\Exception\NoRunningTransactionException;
6
7
final class MultipleTransactionManager extends SimpleTransactionManager
8
{
9
    /**
10
     * @var int number of transactions currently running
11
     */
12
    private $transactionCpt = 0;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 12
    public function beginTransaction()
18
    {
19 12
        ++$this->transactionCpt;
20
21 12
        if ($this->transactionCpt === 1) {
22 12
            parent::beginTransaction();
23 12
        }
24 12
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 9
    public function commit()
30
    {
31 9
        --$this->transactionCpt;
32
33 9
        if ($this->transactionCpt < 0) {
34 3
            throw new NoRunningTransactionException('Cannot commit before a transaction has begun');
35 6
        } elseif ($this->transactionCpt === 0) {
36 3
            parent::commit();
37 3
            $this->reset();
38 3
        }
39 6
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function rollback()
45
    {
46 3
        if ($this->transactionCpt === 0) {
47 3
            return;
48
        }
49
50 3
        parent::rollback();
51 3
        $this->reset();
52 3
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 18
    protected function reset()
58
    {
59 18
        parent::reset();
60 18
        $this->transactionCpt = 0;
61 18
    }
62
}
63