1
|
|
|
<?php |
2
|
|
|
namespace Mathielen\ImportEngine\ValueObject; |
3
|
|
|
|
4
|
|
|
class ImportRun |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const STATE_REVOKED = 'revoked'; |
8
|
|
|
const STATE_FINISHED = 'finished'; |
9
|
|
|
const STATE_CREATED = 'created'; |
10
|
|
|
const STATE_VALIDATED = 'validated'; |
11
|
|
|
|
12
|
|
|
protected $id; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var ImportConfiguration |
16
|
|
|
*/ |
17
|
|
|
protected $configuration; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \DateTime |
21
|
|
|
*/ |
22
|
|
|
protected $createdAt; |
23
|
|
|
protected $createdBy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \DateTime |
27
|
|
|
*/ |
28
|
|
|
protected $validatedAt; |
29
|
|
|
protected $validationMessages; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \DateTime |
33
|
|
|
*/ |
34
|
|
|
protected $finishedAt; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \DateTime |
38
|
|
|
*/ |
39
|
|
|
protected $revokedAt; |
40
|
|
|
protected $revokedBy; |
41
|
|
|
|
42
|
|
|
protected $statistics = array(); |
43
|
|
|
protected $info = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* arbitrary data |
47
|
|
|
*/ |
48
|
|
|
protected $context; |
49
|
|
|
|
50
|
29 |
|
public function __construct(ImportConfiguration $configuration=null, $createdBy = null) |
51
|
|
|
{ |
52
|
29 |
|
$this->id = uniqid(); |
53
|
29 |
|
$this->createdAt = new \DateTime(); |
54
|
29 |
|
$this->configuration = $configuration; |
55
|
29 |
|
$this->createdBy = $createdBy; |
56
|
29 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ImportRun |
60
|
|
|
*/ |
61
|
|
|
public static function create(ImportConfiguration $configuration=null, $createdBy = null) |
62
|
|
|
{ |
63
|
|
|
return new self($configuration, $createdBy); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return ImportRun |
68
|
|
|
*/ |
69
|
1 |
|
public function setContext($context) |
70
|
|
|
{ |
71
|
|
|
//merge existing context with new one, if both are arrays |
72
|
1 |
|
if (is_array($context) && is_array($this->context)) { |
73
|
|
|
$context = array_merge($this->context, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$this->context = $context; |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
public function getContext() |
82
|
|
|
{ |
83
|
6 |
|
return $this->context; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function getId() |
87
|
|
|
{ |
88
|
2 |
|
return $this->id; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getCreatedBy() |
92
|
|
|
{ |
93
|
1 |
|
return $this->createdBy; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ImportRun |
98
|
|
|
*/ |
99
|
1 |
|
public function revoke($revokedBy = null) |
100
|
|
|
{ |
101
|
1 |
|
if (!$this->isFinished()) { |
102
|
|
|
throw new \LogicException('Cannot revoke import if not already finished.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$this->revokedAt = new \DateTime(); |
106
|
1 |
|
$this->revokedBy = $revokedBy; |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return ImportRun |
113
|
|
|
*/ |
114
|
1 |
|
public function reset() |
115
|
|
|
{ |
116
|
1 |
|
$this->finishedAt = null; |
117
|
|
|
|
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
public function isRevoked() |
122
|
|
|
{ |
123
|
4 |
|
return $this->getState() == self::STATE_REVOKED; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return ImportRun |
128
|
|
|
*/ |
129
|
14 |
|
public function finish() |
130
|
|
|
{ |
131
|
14 |
|
$this->finishedAt = new \DateTime(); |
132
|
|
|
|
133
|
14 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
public function isFinished() |
137
|
|
|
{ |
138
|
5 |
|
return $this->getState() == self::STATE_FINISHED; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return ImportRun |
143
|
|
|
*/ |
144
|
1 |
|
public function validated(array $validationMessages=null) |
145
|
|
|
{ |
146
|
1 |
|
$this->validatedAt = empty($this->validatedAt)?new \DateTime():$this->validatedAt; |
147
|
1 |
|
$this->validationMessages = $validationMessages; |
148
|
|
|
|
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function getValidationMessages() |
153
|
|
|
{ |
154
|
1 |
|
return $this->validationMessages; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function isValidated() |
158
|
|
|
{ |
159
|
1 |
|
return $this->isFinished() || $this->getState() == self::STATE_VALIDATED; |
160
|
|
|
} |
161
|
|
|
|
162
|
5 |
|
public function isRunnable() |
163
|
|
|
{ |
164
|
5 |
|
return !$this->isFinished() && !$this->isRevoked(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return ImportConfiguration |
169
|
|
|
*/ |
170
|
1 |
|
public function getConfiguration() |
171
|
|
|
{ |
172
|
1 |
|
return $this->configuration; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return ImportRun |
177
|
|
|
*/ |
178
|
14 |
|
public function setStatistics(array $statistics) |
179
|
|
|
{ |
180
|
14 |
|
$this->statistics = $statistics; |
181
|
|
|
|
182
|
14 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
public function getStatistics() |
186
|
|
|
{ |
187
|
2 |
|
return $this->statistics; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return ImportRun |
192
|
|
|
*/ |
193
|
2 |
|
public function setInfo(array $info) |
194
|
|
|
{ |
195
|
2 |
|
$this->info = $info; |
196
|
|
|
|
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
public function getInfo() |
201
|
|
|
{ |
202
|
1 |
|
return $this->info; |
203
|
|
|
} |
204
|
|
|
|
205
|
17 |
|
public function getState() |
206
|
|
|
{ |
207
|
17 |
|
return self::timestampsToState($this->revokedAt, $this->finishedAt, $this->validatedAt); |
208
|
|
|
} |
209
|
|
|
|
210
|
17 |
|
public static function timestampsToState($revokedAt, $finishedAt, $validatedAt) |
211
|
|
|
{ |
212
|
17 |
|
if (!empty($revokedAt)) { |
213
|
1 |
|
return self::STATE_REVOKED; |
214
|
|
|
} |
215
|
17 |
|
if (!empty($finishedAt)) { |
216
|
13 |
|
return self::STATE_FINISHED; |
217
|
|
|
} |
218
|
16 |
|
if (!empty($validatedAt)) { |
219
|
1 |
|
return self::STATE_VALIDATED; |
220
|
|
|
} |
221
|
|
|
|
222
|
16 |
|
return self::STATE_CREATED; |
223
|
|
|
} |
224
|
|
|
|
225
|
12 |
|
public function toArray() |
226
|
|
|
{ |
227
|
|
|
return array( |
228
|
12 |
|
'id' => $this->id, |
229
|
12 |
|
'configuration' => $this->configuration?$this->configuration->toArray():null, |
230
|
12 |
|
'statistics' => $this->statistics, |
231
|
12 |
|
'created_by' => $this->createdBy, |
232
|
12 |
|
'created_at' => $this->createdAt->getTimestamp(), |
233
|
12 |
|
'revoked_by' => $this->revokedBy, |
234
|
12 |
|
'revoked_at' => $this->revokedAt?$this->revokedAt->getTimestamp():null, |
235
|
12 |
|
'finished_at' => $this->finishedAt?$this->finishedAt->getTimestamp():null, |
236
|
12 |
|
'state' => $this->getState() |
237
|
12 |
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|