Completed
Push — master ( adbc36...3633f5 )
by Rémi
03:10
created

MultipleTransactionManager::commit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace RemiSan\TransactionManager;
4
5
use RemiSan\TransactionManager\Exception\NoRunningTransactionException;
6
7
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
    public function beginTransaction()
18
    {
19
        $this->transactionCpt++;
20
21
        if ($this->transactionCpt === 1) {
22
            parent::beginTransaction();
23
        }
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function commit()
30
    {
31
        $this->transactionCpt--;
32
33
        if ($this->transactionCpt < 0) {
34
            throw new NoRunningTransactionException('Cannot commit before a transaction has begun');
35
        } elseif ($this->transactionCpt === 0) {
36
            parent::commit();
37
            $this->reset();
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function rollback()
45
    {
46
        if ($this->transactionCpt === 0) {
47
            return;
48
        }
49
50
        parent::rollback();
51
        $this->reset();
52
    }
53
54
    /**
55
     * Reset the transaction.
56
     */
57
    private function reset()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
58
    {
59
        $this->transactionCpt = 0;
60
    }
61
}
62