1 | <?php |
||
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) |
||
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() |
|
85 | |||
86 | 2 | public function getId() |
|
90 | |||
91 | 1 | public function getCreatedBy() |
|
95 | |||
96 | /** |
||
97 | * @return ImportRun |
||
98 | */ |
||
99 | 1 | public function revoke($revokedBy = null) |
|
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() |
|
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() |
|
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() |
|
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() |
|
189 | |||
190 | /** |
||
191 | * @return ImportRun |
||
192 | */ |
||
193 | 2 | public function setInfo(array $info) |
|
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); |
|
209 | |||
210 | 17 | public static function timestampsToState($revokedAt, $finishedAt, $validatedAt) |
|
224 | |||
225 | 12 | public function toArray() |
|
239 | |||
240 | } |
||
241 |