1 | <?php |
||
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 |
|
82 | |||
83 | /** |
||
84 | * Set the process result |
||
85 | * |
||
86 | * @param $result |
||
87 | * @return $this |
||
88 | */ |
||
89 | 9 | protected function putResult($result) |
|
94 | |||
95 | /** |
||
96 | * Retrieve the result |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 6 | public function getResult() |
|
104 | |||
105 | /** |
||
106 | * Execute the process |
||
107 | */ |
||
108 | 11 | public function execute() |
|
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 |
|
138 | } |
||
139 |