MultipleTransactionManager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 56
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beginTransaction() 0 8 2
A commit() 0 11 3
A rollback() 0 9 2
A reset() 0 5 1
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