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

Process::rollBack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
namespace NeedleProject\Transaction;
3
4
/**
5
 * Class Process
6
 *
7
 * @package NeedleProject\Transaction
8
 * @author  Adrian Tilita <[email protected]>
9
 */
10
class Process implements ProcessInterface
11
{
12
    /**
13
     * @var null|\Closure
14
     */
15
    private $executeAction = null;
16
17
    /**
18
     * @var null|\Closure
19
     */
20
    private $rollbackAction = null;
21
22
    /**
23
     * @var string
24
     */
25
    private $processName = null;
26
27
    /**
28
     * @var bool
29
     */
30
    private $executed = false;
31
32
    /**
33
     * @var null
34
     */
35
    private $processResult = null;
36
37
    /**
38
     * Process constructor.
39
     *
40
     * @param \Closure $executeAction
41
     * @param \Closure $rollbackAction
42
     * @param string $processName
43
     */
44 17
    protected function __construct(\Closure $executeAction, \Closure $rollbackAction, string $processName = null)
45
    {
46 17
        if (is_null($processName)) {
47 4
            $processName = spl_object_hash($executeAction) . '_' . spl_object_hash($rollbackAction);
48
        }
49 17
        $this->executeAction = $executeAction;
50 17
        $this->rollbackAction = $rollbackAction;
51 17
        $this->processName = $processName;
52 17
    }
53
54
    /**
55
     * Create a new Process
56
     *
57
     * @param \Closure $executeAction
58
     * @param \Closure $rollbackAction
59
     * @param string $processName
60
     * @param bool $debug
61
     * @return ProcessInterface
62
     */
63 17
    public static function createProcess(
64
        \Closure $executeAction,
65
        \Closure $rollbackAction,
66
        string $processName = null,
67
        bool $debug = false
68
    ): ProcessInterface {
69 17
        if (true === $debug) {
70 3
            return new MonitoredProcess($executeAction, $rollbackAction, $processName);
71
        }
72 14
        return new self($executeAction, $rollbackAction, $processName);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 3
    public function getName(): string
79
    {
80 3
        return $this->processName;
81
    }
82
83
    /**
84
     * Set the process result
85
     *
86
     * @param $result
87
     * @return $this
88
     */
89 9
    protected function putResult($result)
90
    {
91 9
        $this->processResult = $result;
92 9
        return $this;
93
    }
94
95
    /**
96
     * Retrieve the result
97
     *
98
     * @return mixed
99
     */
100 6
    public function getResult()
101
    {
102 6
        return $this->processResult;
103
    }
104
105
    /**
106
     * Execute the process
107
     */
108 11
    public function execute()
109
    {
110 11
        $this->putResult($this->executeAction->__invoke());
111 9
        $this->executed = true;
112 9
        return $this;
113
    }
114
115
    /**
116
     * Rollback action
117
     */
118 5
    public function rollBack()
119
    {
120 5
        if (false === $this->hasExecuted()) {
121 1
            throw new \RuntimeException(
122 1
                sprintf("Process %s cannot be roll-backed because was not executed!", $this->getName())
123
            );
124
        }
125
126 4
        $this->putResult($this->rollbackAction->__invoke());
127 4
        $this->executed = false;
128 4
        return $this;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134 8
    public function hasExecuted(): bool
135
    {
136 8
        return $this->executed;
137
    }
138
}
139