Completed
Push — master ( 462e77...dac8ca )
by Rémi
03:50 queued 43s
created

SimpleTransactionManager::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace RemiSan\TransactionManager;
4
5
use RemiSan\TransactionManager\Exception\BeginException;
6
use RemiSan\TransactionManager\Exception\NoRunningTransactionException;
7
use RemiSan\TransactionManager\Exception\TransactionException;
8
9
class SimpleTransactionManager implements TransactionManager
10
{
11
    /**
12
     * @var Transactional[]
13
     */
14
    private $items;
15
16
    /**
17
     * @var int
18
     */
19
    private $running;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param Transactional[] $items
25
     */
26 27
    public function __construct(array $items = [])
27
    {
28 27
        $this->items = $items;
29 27
        $this->reset();
30 27
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 15
    public function addTransactionalItem(Transactional $item)
36
    {
37 15
        if ($this->running) {
38 3
            throw new TransactionException('You cannot add a transactional item during a running transaction');
39
        }
40
41 15
        $this->items[] = $item;
42 15
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 18
    public function beginTransaction()
48
    {
49 18
        if ($this->running) {
50 3
            throw new BeginException('Transaction already running');
51
        }
52
53 18
        if (count($this->items) === 0) {
54 3
            throw new BeginException('No transaction to start');
55
        }
56
57 15
        foreach ($this->items as $item) {
58 15
            $item->beginTransaction();
59 10
        }
60
61 15
        $this->running = true;
0 ignored issues
show
Documentation Bug introduced by
The property $running was declared of type integer, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
62 15
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 6
    public function commit()
68
    {
69 6
        $this->checkTransaction();
70
71 3
        foreach ($this->items as $item) {
72 3
            $item->commit();
73 2
        }
74
75 3
        $this->reset();
76 3
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 6
    public function rollback()
82
    {
83 6
        $this->checkTransaction();
84
85 3
        foreach ($this->items as $item) {
86 3
            $item->rollback();
87 2
        }
88
89 3
        $this->reset();
90 3
    }
91
92
    /**
93
     * Check if there's a transaction running.
94
     *
95
     * @throws NoRunningTransactionException
96
     */
97 12
    private function checkTransaction()
98
    {
99 12
        if (!$this->running) {
100 6
            throw new NoRunningTransactionException('No transaction running');
101
        }
102 6
    }
103
104
    /**
105
     * Reset the transactions.
106
     */
107 27
    private function reset()
108
    {
109 27
        $this->running = false;
0 ignored issues
show
Documentation Bug introduced by
The property $running was declared of type integer, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
110 27
    }
111
}
112