Completed
Push — master ( e03476...93f10a )
by Tilita
02:03
created

Executor::rollBack()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3
1
<?php
2
namespace NeedleProject\Transaction;
3
4
/**
5
 * Class Executor
6
 *
7
 * @package NeedleProject\Transaction
8
 * @author  Adrian Tilita <[email protected]>
9
 */
10
class Executor
11
{
12
    /**
13
     * @var \ArrayIterator|null
14
     */
15
    private $processList = null;
16
17
    /**
18
     * Executor constructor.
19
     */
20 6
    public function __construct()
21
    {
22 6
        $this->processList = new \ArrayIterator();
23 6
    }
24
25
    /**
26
     * @param ProcessInterface $process
27
     */
28 6
    public function addProcess(ProcessInterface $process)
29
    {
30 6
        $this->processList->append($process);
31 6
    }
32
33
    /**
34
     * Execute a set of processes
35
     */
36 6
    public function execute()
37
    {
38 6
        $this->processList->rewind();
39 6
        while ($this->processList->valid()) {
40 6
            $this->processList->current()->execute();
41 4
            $this->processList->next();
42
        }
43 3
    }
44
45
    /**
46
     * Rollback in reverse order
47
     */
48 3
    public function rollBack()
49
    {
50 3
        $maxOffset = $this->processList->count() - 1;
51
52 3
        for ($i = $maxOffset; $i >= 0; $i--) {
53
            /** @var ProcessInterface $currentProcess */
54 3
            $currentProcess = $this->processList->offsetGet($i);
55
            // Exclude rolling back processes that has not been executed
56 3
            if ($currentProcess->hasExecuted() === false) {
0 ignored issues
show
Bug introduced by
The method hasExecuted() does not exist on NeedleProject\Transaction\ProcessInterface. Did you maybe mean execute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57 2
                continue;
58
            }
59 2
            $currentProcess->rollBack();
60
        }
61 3
    }
62
}
63