1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\ProcessBundle\State; |
6
|
|
|
|
7
|
|
|
use Psr\Log\AbstractLogger; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Darkilliant\ProcessBundle\Runner\StepRunner; |
10
|
|
|
|
11
|
|
|
class ProcessState extends AbstractLogger |
12
|
|
|
{ |
13
|
|
|
const RESULT_KO = 1; |
14
|
|
|
const RESULT_SKIP = 2; |
15
|
|
|
const RESULT_OK = 3; |
16
|
|
|
const RESULT_BREAK = 4; |
17
|
|
|
const RESULT_EXIT = 5; |
18
|
|
|
|
19
|
|
|
private $data; |
20
|
|
|
private $context; |
21
|
|
|
private $options = []; |
22
|
|
|
private $logger; |
23
|
|
|
private $result; |
24
|
|
|
|
25
|
|
|
private $dryRun = false; |
26
|
|
|
|
27
|
|
|
/** @var StepRunner */ |
28
|
|
|
private $stepRunner; |
29
|
|
|
|
30
|
|
|
/** @var \Traversable */ |
31
|
|
|
private $iterator; |
32
|
|
|
|
33
|
|
|
private $loop; |
34
|
|
|
|
35
|
131 |
|
public function __construct(array $context, LoggerInterface $logger, StepRunner $stepRunner) |
36
|
|
|
{ |
37
|
131 |
|
$this->context = $context; |
38
|
131 |
|
$this->logger = $logger; |
39
|
131 |
|
$this->stepRunner = $stepRunner; |
40
|
131 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
36 |
|
public function getData() |
46
|
|
|
{ |
47
|
36 |
|
return $this->data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $data |
52
|
|
|
*/ |
53
|
38 |
|
public function setData($data) |
54
|
|
|
{ |
55
|
38 |
|
$this->data = $data; |
56
|
38 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
8 |
|
public function getContext($key) |
62
|
|
|
{ |
63
|
8 |
|
return $this->context[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
public function getLoop() |
67
|
|
|
{ |
68
|
3 |
|
return $this->loop; |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
public function noLoop() |
72
|
|
|
{ |
73
|
4 |
|
$this->loop = null; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
8 |
|
public function loop(int $index, int $count, bool $last) |
77
|
|
|
{ |
78
|
8 |
|
$this->loop = [ |
79
|
8 |
|
'index' => $index, |
80
|
8 |
|
'count' => $count, |
81
|
8 |
|
'last' => $last, |
82
|
|
|
]; |
83
|
8 |
|
} |
84
|
|
|
|
85
|
2 |
|
public function isLoop() |
86
|
|
|
{ |
87
|
2 |
|
return (bool) $this->loop; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $context |
92
|
|
|
*/ |
93
|
14 |
|
public function setContext($key, $value) |
94
|
|
|
{ |
95
|
14 |
|
$this->context[$key] = $value; |
96
|
14 |
|
} |
97
|
|
|
|
98
|
8 |
|
public function getRawContext(): array |
99
|
|
|
{ |
100
|
8 |
|
return $this->context; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return LoggerInterface |
105
|
|
|
*/ |
106
|
5 |
|
public function getLogger(): LoggerInterface |
107
|
|
|
{ |
108
|
5 |
|
return $this->logger; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
78 |
|
public function getOptions(): array |
115
|
|
|
{ |
116
|
78 |
|
return $this->options; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $options |
121
|
|
|
*/ |
122
|
86 |
|
public function setOptions(array $options): ProcessState |
123
|
|
|
{ |
124
|
86 |
|
$this->options = $options; |
125
|
|
|
|
126
|
86 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
36 |
|
public function log($level, $message, array $context = []) |
130
|
|
|
{ |
131
|
36 |
|
$this->logger->log($level, $message, array_merge($context, $this->context)); |
132
|
36 |
|
} |
133
|
|
|
|
134
|
25 |
|
public function getResult() |
135
|
|
|
{ |
136
|
25 |
|
return $this->result; |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
public function markFail() |
140
|
|
|
{ |
141
|
5 |
|
$this->result = self::RESULT_KO; |
142
|
5 |
|
} |
143
|
|
|
|
144
|
4 |
|
public function markIgnore() |
145
|
|
|
{ |
146
|
4 |
|
$this->result = self::RESULT_SKIP; |
147
|
4 |
|
} |
148
|
|
|
|
149
|
20 |
|
public function markSuccess() |
150
|
|
|
{ |
151
|
20 |
|
$this->result = self::RESULT_OK; |
152
|
20 |
|
} |
153
|
|
|
|
154
|
5 |
|
public function markBreak() |
155
|
|
|
{ |
156
|
5 |
|
$this->result = self::RESULT_BREAK; |
157
|
5 |
|
} |
158
|
|
|
|
159
|
1 |
|
public function markExit() |
160
|
|
|
{ |
161
|
1 |
|
$this->result = self::RESULT_EXIT; |
162
|
1 |
|
} |
163
|
|
|
|
164
|
3 |
|
public function getStepRunner(): StepRunner |
165
|
|
|
{ |
166
|
3 |
|
return $this->stepRunner; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
public function duplicate($logger = null): self |
170
|
|
|
{ |
171
|
1 |
|
$duplicate = new self($this->context, $logger ?? $this->logger, $this->stepRunner); |
172
|
1 |
|
$duplicate->setData($this->data); |
173
|
|
|
|
174
|
1 |
|
return $duplicate; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return \ArrayIterator |
179
|
|
|
*/ |
180
|
19 |
|
public function getIterator() |
181
|
|
|
{ |
182
|
19 |
|
return $this->iterator; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param \Traversable $iterator |
187
|
|
|
*/ |
188
|
23 |
|
public function setIterator($iterator) |
189
|
|
|
{ |
190
|
23 |
|
$this->iterator = $iterator; |
191
|
23 |
|
} |
192
|
|
|
|
193
|
15 |
|
public function setDryRun(bool $dryRun) |
194
|
|
|
{ |
195
|
15 |
|
$this->dryRun = $dryRun; |
196
|
15 |
|
} |
197
|
|
|
|
198
|
10 |
|
public function isDryRun(): bool |
199
|
|
|
{ |
200
|
10 |
|
return $this->dryRun; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|