|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Subjects\ExportableTrait |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Adapter\ExportAdapterInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The trait implementation for the artefact export functionality. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
trait ExportableTrait |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The array containing the data for product type configuration (configurables, bundles, etc). |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $artefacts = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The export adapter instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \TechDivision\Import\Adapter\ExportAdapterInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $exportAdapter; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The ID of the product that has been created recently. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $lastEntityId; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return's the artefacts for post-processing. |
|
61
|
|
|
* |
|
62
|
|
|
* @return array The artefacts |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public function getArtefacts() |
|
65
|
|
|
{ |
|
66
|
4 |
|
return $this->artefacts; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Reset the array with the artefacts to free the memory. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function resetArtefacts() |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->artefacts = array(); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Add the passed product type artefacts to the product with the |
|
81
|
|
|
* last entity ID. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
84
|
|
|
* @param array $artefacts The product type artefacts |
|
85
|
|
|
* @param boolean $override Whether or not the artefacts for the actual entity ID has to be overwritten |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
* @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function addArtefacts($type, array $artefacts, $override = true) |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
// query whether or not, any artefacts are available |
|
94
|
5 |
|
if (sizeof($artefacts) === 0) { |
|
95
|
1 |
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// serialize the original data, if we're in debug mode |
|
99
|
4 |
|
$keys = array_keys($artefacts); |
|
100
|
4 |
|
foreach ($keys as $key) { |
|
101
|
4 |
|
if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) { |
|
102
|
4 |
|
$artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? serialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// query whether or not, existing artefacts has to be overwritten |
|
107
|
4 |
|
if ($override === true) { |
|
108
|
4 |
|
$this->overrideArtefacts($type, $artefacts); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->appendArtefacts($type, $artefacts); |
|
111
|
|
|
} |
|
112
|
4 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Add the passed product type artefacts to the product with the |
|
116
|
|
|
* last entity ID and overrides existing ones with the same key. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
119
|
|
|
* @param array $artefacts The product type artefacts |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
4 |
|
protected function overrideArtefacts($type, array $artefacts) |
|
124
|
|
|
{ |
|
125
|
4 |
|
foreach ($artefacts as $key => $artefact) { |
|
126
|
4 |
|
$this->artefacts[$type][$this->getLastEntityId()][$key] = $artefact; |
|
127
|
|
|
} |
|
128
|
4 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Append's the passed product type artefacts to the product with the |
|
132
|
|
|
* last entity ID. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
135
|
|
|
* @param array $artefacts The product type artefacts |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function appendArtefacts($type, array $artefacts) |
|
140
|
|
|
{ |
|
141
|
|
|
foreach ($artefacts as $artefact) { |
|
142
|
|
|
$this->artefacts[$type][$this->getLastEntityId()][] = $artefact; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Return the artefacts for the passed type and entity ID. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
150
|
|
|
* @param string $entityId The entity ID to return the artefacts for |
|
151
|
|
|
* |
|
152
|
|
|
* @return array The array with the artefacts |
|
153
|
|
|
* @throws \Exception Is thrown, if no artefacts are available |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function getArtefactsByTypeAndEntityId($type, $entityId) |
|
156
|
|
|
{ |
|
157
|
|
|
|
|
158
|
|
|
// query whether or not, artefacts for the passed params are available |
|
159
|
2 |
|
if (isset($this->artefacts[$type][$entityId])) { |
|
160
|
|
|
// load the artefacts |
|
161
|
1 |
|
$artefacts = $this->artefacts[$type][$entityId]; |
|
162
|
|
|
|
|
163
|
|
|
// unserialize the original data, if we're in debug mode, if we're in debug mode |
|
164
|
1 |
|
$keys = array_keys($artefacts); |
|
165
|
1 |
|
foreach ($keys as $key) { |
|
166
|
1 |
|
if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) { |
|
167
|
1 |
|
$artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? unserialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// return the artefacts |
|
172
|
1 |
|
return $artefacts; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// throw an exception if not |
|
176
|
1 |
|
throw new \Exception( |
|
177
|
1 |
|
sprintf( |
|
178
|
1 |
|
'Cant\'t load artefacts for type %s and entity ID %d', |
|
179
|
1 |
|
$type, |
|
180
|
1 |
|
$entityId |
|
181
|
|
|
) |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Queries whether or not artefacts for the passed type and entity ID are available. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
189
|
|
|
* @param string $entityId The entity ID to return the artefacts for |
|
190
|
|
|
* |
|
191
|
|
|
* @return boolean TRUE if artefacts are available, else FALSE |
|
192
|
|
|
*/ |
|
193
|
|
|
public function hasArtefactsByTypeAndEntityId($type, $entityId) |
|
194
|
|
|
{ |
|
195
|
|
|
return isset($this->artefacts[$type][$entityId]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Create's and return's a new empty artefact entity. |
|
200
|
|
|
* |
|
201
|
|
|
* @param array $columns The array with the column data |
|
202
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
|
203
|
|
|
* |
|
204
|
|
|
* @return array The new artefact entity |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function newArtefact(array $columns, array $originalColumnNames = array()) |
|
207
|
|
|
{ |
|
208
|
|
|
|
|
209
|
|
|
// initialize the original data and the artefact |
|
210
|
1 |
|
$artefact = array(); |
|
211
|
1 |
|
$originalData = array(); |
|
212
|
|
|
|
|
213
|
|
|
// query whether or not, we've original columns |
|
214
|
1 |
|
if (sizeof($originalColumnNames) > 0) { |
|
215
|
|
|
// prepare the original column data |
|
216
|
1 |
|
$originalData[ColumnKeys::ORIGINAL_FILENAME] = $this->getFilename(); |
|
|
|
|
|
|
217
|
1 |
|
$originalData[ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getLineNumber(); |
|
|
|
|
|
|
218
|
1 |
|
$originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES] = $originalColumnNames; |
|
219
|
|
|
|
|
220
|
|
|
// add the original column data to the new artefact |
|
221
|
1 |
|
$artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// merge the columns into the artefact entity and return it |
|
225
|
1 |
|
return array_merge($artefact, $columns); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Export's the artefacts to CSV files and resets the array with the artefacts to free the memory. |
|
230
|
|
|
* |
|
231
|
|
|
* @param integer $timestamp The timestamp part of the original import file |
|
232
|
|
|
* @param string $counter The counter part of the origin import file |
|
233
|
|
|
* |
|
234
|
|
|
* @return void |
|
235
|
|
|
*/ |
|
236
|
1 |
|
public function export($timestamp, $counter) |
|
237
|
|
|
{ |
|
238
|
|
|
|
|
239
|
|
|
// export the artefacts |
|
240
|
1 |
|
$this->getExportAdapter()->export($this->getArtefacts(), $this->getTargetDir(), $timestamp, $counter); |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
// reset the artefacts |
|
243
|
1 |
|
$this->resetArtefacts(); |
|
244
|
1 |
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Set's the exporter adapter instance. |
|
248
|
|
|
* |
|
249
|
|
|
* @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance |
|
250
|
|
|
* |
|
251
|
|
|
* @return void |
|
252
|
|
|
*/ |
|
253
|
2 |
|
public function setExportAdapter(ExportAdapterInterface $exportAdapter) |
|
254
|
|
|
{ |
|
255
|
2 |
|
$this->exportAdapter = $exportAdapter; |
|
256
|
2 |
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Return's the exporter adapter instance. |
|
260
|
|
|
* |
|
261
|
|
|
* @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance |
|
262
|
|
|
*/ |
|
263
|
2 |
|
public function getExportAdapter() |
|
264
|
|
|
{ |
|
265
|
2 |
|
return $this->exportAdapter; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Set's the ID of the product that has been created recently. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $lastEntityId The entity ID |
|
272
|
|
|
* |
|
273
|
|
|
* @return void |
|
274
|
|
|
*/ |
|
275
|
4 |
|
public function setLastEntityId($lastEntityId) |
|
276
|
|
|
{ |
|
277
|
4 |
|
$this->lastEntityId = $lastEntityId; |
|
278
|
4 |
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Return's the ID of the product that has been created recently. |
|
282
|
|
|
* |
|
283
|
|
|
* @return string The entity Id |
|
284
|
|
|
*/ |
|
285
|
4 |
|
public function getLastEntityId() |
|
286
|
|
|
{ |
|
287
|
4 |
|
return $this->lastEntityId; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Queries whether or not debug mode is enabled or not, default is TRUE. |
|
292
|
|
|
* |
|
293
|
|
|
* @return boolean TRUE if debug mode is enabled, else FALSE |
|
294
|
|
|
*/ |
|
295
|
|
|
abstract public function isDebugMode(); |
|
296
|
|
|
} |
|
297
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.