|
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 $artefacs = 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
|
3 |
|
public function getArtefacts() |
|
65
|
|
|
{ |
|
66
|
3 |
|
return $this->artefacs; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add the passed product type artefacts to the product with the |
|
71
|
|
|
* last entity ID. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
74
|
|
|
* @param array $artefacts The product type artefacts |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
* @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function addArtefacts($type, array $artefacts) |
|
80
|
|
|
{ |
|
81
|
|
|
|
|
82
|
|
|
// query whether or not, any artefacts are available |
|
83
|
4 |
|
if (sizeof($artefacts) === 0) { |
|
84
|
1 |
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// serialize the original data |
|
88
|
3 |
|
array_walk($artefacts, function(&$artefact) { |
|
|
|
|
|
|
89
|
3 |
|
if (isset($artefact[ColumnKeys::ORIGINAL_DATA])) { |
|
90
|
1 |
|
$artefact[ColumnKeys::ORIGINAL_DATA] = serialize($artefact[ColumnKeys::ORIGINAL_DATA]); |
|
91
|
1 |
|
} |
|
92
|
3 |
|
}); |
|
93
|
|
|
|
|
94
|
|
|
// append the artefacts to the stack |
|
95
|
3 |
|
$this->artefacs[$type][$this->getLastEntityId()][] = $artefacts; |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the artefacts for the passed type and entity ID. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
102
|
|
|
* @param string $entityId The entity ID to return the artefacts for |
|
103
|
|
|
* |
|
104
|
|
|
* @return array The array with the artefacts |
|
105
|
|
|
* @throws \Exception Is thrown, if no artefacts are available |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function getArtefactsByTypeAndEntityId($type, $entityId) |
|
108
|
|
|
{ |
|
109
|
|
|
|
|
110
|
|
|
// query whether or not, artefacts for the passed params are available |
|
111
|
2 |
|
if (isset($this->artefacs[$type][$entityId])) { |
|
112
|
1 |
|
return reset($this->artefacs[$type][$entityId]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// throw an exception if not |
|
116
|
1 |
|
throw new \Exception( |
|
117
|
1 |
|
sprintf( |
|
118
|
1 |
|
'Cant\'t load artefacts for type %s and entity ID %d', |
|
119
|
1 |
|
$type, |
|
120
|
|
|
$entityId |
|
121
|
1 |
|
) |
|
122
|
1 |
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create's and return's a new empty artefact entity. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $columns The array with the column data |
|
129
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
|
130
|
|
|
* |
|
131
|
|
|
* @return array The new artefact entity |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function newArtefact(array $columns, array $originalColumnNames = array()) |
|
134
|
|
|
{ |
|
135
|
|
|
|
|
136
|
|
|
// initialize the original data |
|
137
|
|
|
$originalData = array( |
|
138
|
1 |
|
ColumnKeys::ORIGINAL_FILENAME => $this->getFilename(), |
|
|
|
|
|
|
139
|
1 |
|
ColumnKeys::ORIGINAL_LINE_NUMBER => $this->getLineNumber(), |
|
|
|
|
|
|
140
|
1 |
|
ColumnKeys::ORIGINAL_COLUMN_NAMES => $originalColumnNames |
|
141
|
1 |
|
); |
|
142
|
|
|
|
|
143
|
|
|
// prepare a new artefact entity |
|
144
|
1 |
|
$artefact = array(ColumnKeys::ORIGINAL_DATA => $originalData); |
|
145
|
|
|
|
|
146
|
|
|
// merge the columns into the artefact entity and return it |
|
147
|
1 |
|
return array_merge($artefact, $columns); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Export's the artefacts to CSV files. |
|
152
|
|
|
* |
|
153
|
|
|
* @param integer $timestamp The timestamp part of the original import file |
|
154
|
|
|
* @param string $counter The counter part of the origin import file |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function export($timestamp, $counter) |
|
159
|
|
|
{ |
|
160
|
1 |
|
$this->getExportAdapter()->export($this->getArtefacts(), $this->getTargetDir(), $timestamp, $counter); |
|
|
|
|
|
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set's the exporter adapter instance. |
|
165
|
|
|
* |
|
166
|
|
|
* @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function setExportAdapter(ExportAdapterInterface $exportAdapter) |
|
171
|
|
|
{ |
|
172
|
2 |
|
$this->exportAdapter = $exportAdapter; |
|
173
|
2 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return's the exporter adapter instance. |
|
177
|
|
|
* |
|
178
|
|
|
* @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance |
|
179
|
|
|
*/ |
|
180
|
2 |
|
public function getExportAdapter() |
|
181
|
|
|
{ |
|
182
|
2 |
|
return $this->exportAdapter; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Set's the ID of the product that has been created recently. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $lastEntityId The entity ID |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
3 |
|
public function setLastEntityId($lastEntityId) |
|
193
|
|
|
{ |
|
194
|
3 |
|
$this->lastEntityId = $lastEntityId; |
|
195
|
3 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Return's the ID of the product that has been created recently. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string The entity Id |
|
201
|
|
|
*/ |
|
202
|
3 |
|
public function getLastEntityId() |
|
203
|
|
|
{ |
|
204
|
3 |
|
return $this->lastEntityId; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|