|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Media\Subjects\MediaSubject |
|
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-product-media |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Media\Subjects; |
|
22
|
|
|
|
|
23
|
|
|
use League\Flysystem\Filesystem; |
|
24
|
|
|
use League\Flysystem\Adapter\Local; |
|
25
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
|
26
|
|
|
use TechDivision\Import\Subjects\FileUploadTrait; |
|
27
|
|
|
use TechDivision\Import\Subjects\FileUploadSubjectInterface; |
|
28
|
|
|
use TechDivision\Import\Product\Media\Utils\ConfigurationKeys; |
|
29
|
|
|
use TechDivision\Import\Product\Subjects\AbstractProductSubject; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The subject implementation for the product media handling. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Tim Wagner <[email protected]> |
|
35
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
37
|
|
|
* @link https://github.com/techdivision/import-product-media |
|
38
|
|
|
* @link http://www.techdivision.com |
|
39
|
|
|
*/ |
|
40
|
|
|
class MediaSubject extends AbstractProductSubject implements FileUploadSubjectInterface |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The trait that provides file upload functionality. |
|
45
|
|
|
* |
|
46
|
|
|
* @var \TechDivision\Import\Subjects\FileUploadTrait |
|
47
|
|
|
*/ |
|
48
|
|
|
use FileUploadTrait; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The ID of the parent product to relate the variant with. |
|
52
|
|
|
* |
|
53
|
|
|
* @var integer |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $parentId; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The value ID of the created media gallery entry. |
|
59
|
|
|
* |
|
60
|
|
|
* @var integer |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $parentValueId; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The Magento installation directory. |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $installationDir; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The position counter, if no position for the product media gallery value has been specified. |
|
73
|
|
|
* |
|
74
|
|
|
* @var integer |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $positionCounter = 1; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The available stores. |
|
80
|
|
|
* |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $stores = array(); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The mapping for the SKUs to the created entity IDs. |
|
87
|
|
|
* |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $skuEntityIdMapping = array(); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Intializes the previously loaded global data for exactly one variants. |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
* @see \Importer\Csv\Actions\ProductImportAction::prepare() |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setUp() |
|
99
|
|
|
{ |
|
100
|
|
|
|
|
101
|
|
|
// invoke parent method |
|
102
|
|
|
parent::setUp(); |
|
103
|
|
|
|
|
104
|
|
|
// load the entity manager and the registry processor |
|
105
|
|
|
$registryProcessor = $this->getRegistryProcessor(); |
|
106
|
|
|
|
|
107
|
|
|
// load the status of the actual import process |
|
108
|
|
|
$status = $registryProcessor->getAttribute($this->getSerial()); |
|
109
|
|
|
|
|
110
|
|
|
// load the attribute set we've prepared intially |
|
111
|
|
|
$this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING]; |
|
112
|
|
|
|
|
113
|
|
|
// initialize the flag to decide copy images or not |
|
114
|
|
|
$this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
// initialize the filesystems root directory |
|
117
|
|
|
$this->setRootDir($this->getConfiguration()->getParam(ConfigurationKeys::ROOT_DIRECTORY, getcwd())); |
|
118
|
|
|
|
|
119
|
|
|
// initialize the filesystem |
|
120
|
|
|
$this->setFilesystem(new Filesystem(new Local($this->getRootDir()))); |
|
121
|
|
|
|
|
122
|
|
|
// initialize media directory => can be absolute or relative |
|
123
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
|
124
|
|
|
$this->setMediaDir( |
|
125
|
|
|
$this->resolvePath( |
|
126
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY) |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// initialize images directory => can be absolute or relative |
|
132
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
|
133
|
|
|
$this->setImagesFileDir( |
|
134
|
|
|
$this->resolvePath( |
|
135
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY) |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Set's the ID of the parent product to relate the variant with. |
|
143
|
|
|
* |
|
144
|
|
|
* @param integer $parentId The ID of the parent product |
|
145
|
|
|
* |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setParentId($parentId) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->parentId = $parentId; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Return's the ID of the parent product to relate the variant with. |
|
155
|
|
|
* |
|
156
|
|
|
* @return integer The ID of the parent product |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getParentId() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->parentId; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set's the value ID of the created media gallery entry. |
|
165
|
|
|
* |
|
166
|
|
|
* @param integer $parentValueId The ID of the created media gallery entry |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setParentValueId($parentValueId) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->parentValueId = $parentValueId; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return's the value ID of the created media gallery entry. |
|
177
|
|
|
* |
|
178
|
|
|
* @return integer The ID of the created media gallery entry |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getParentValueId() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->parentValueId; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Reset the position counter to 1. |
|
187
|
|
|
* |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
|
|
public function resetPositionCounter() |
|
191
|
|
|
{ |
|
192
|
|
|
$this->positionCounter = 1; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Returns the acutal value of the position counter and raise's it by one. |
|
197
|
|
|
* |
|
198
|
|
|
* @return integer The actual value of the position counter |
|
199
|
|
|
*/ |
|
200
|
|
|
public function raisePositionCounter() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->positionCounter++; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Return the entity ID for the passed SKU. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
209
|
|
|
* |
|
210
|
|
|
* @return integer The mapped entity ID |
|
211
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
|
212
|
|
|
*/ |
|
213
|
|
|
public function mapSkuToEntityId($sku) |
|
214
|
|
|
{ |
|
215
|
|
|
|
|
216
|
|
|
// query weather or not the SKU has been mapped |
|
217
|
|
|
if (isset($this->skuEntityIdMapping[$sku])) { |
|
218
|
|
|
return $this->skuEntityIdMapping[$sku]; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// throw an exception if the SKU has not been mapped yet |
|
222
|
|
|
throw new \Exception(sprintf('Found not mapped SKU %s', $sku)); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Return's the store for the passed store code. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $storeCode The store code to return the store for |
|
229
|
|
|
* |
|
230
|
|
|
* @return array The requested store |
|
231
|
|
|
* @throws \Exception Is thrown, if the requested store is not available |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getStoreByStoreCode($storeCode) |
|
234
|
|
|
{ |
|
235
|
|
|
|
|
236
|
|
|
// query whether or not the store with the passed store code exists |
|
237
|
|
|
if (isset($this->stores[$storeCode])) { |
|
238
|
|
|
return $this->stores[$storeCode]; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// throw an exception, if not |
|
242
|
|
|
throw new \Exception(sprintf('Found invalid store code %s', $storeCode)); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Load's the product media gallery with the passed attribute ID + value. |
|
247
|
|
|
* |
|
248
|
|
|
* @param integer $attributeId The attribute ID of the product media gallery to load |
|
249
|
|
|
* @param string $value The value of the product media gallery to load |
|
250
|
|
|
* |
|
251
|
|
|
* @return array The product media gallery |
|
252
|
|
|
*/ |
|
253
|
|
|
public function loadProductMediaGallery($attributeId, $value) |
|
254
|
|
|
{ |
|
255
|
|
|
return $this->getProductProcessor()->loadProductMediaGallery($attributeId, $value); |
|
|
|
|
|
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Load's the product media gallery with the passed value/entity ID. |
|
260
|
|
|
* |
|
261
|
|
|
* @param integer $valueId The value ID of the product media gallery value to entity to load |
|
262
|
|
|
* @param integer $entityId The entity ID of the product media gallery value to entity to load |
|
263
|
|
|
* |
|
264
|
|
|
* @return array The product media gallery |
|
265
|
|
|
*/ |
|
266
|
|
|
public function loadProductMediaGalleryValueToEntity($valueId, $entityId) |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->getProductProcessor()->loadProductMediaGalleryValueToEntity($valueId, $entityId); |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Load's the product media gallery value with the passed value/store/parent ID. |
|
273
|
|
|
* |
|
274
|
|
|
* @param integer $valueId The value ID of the product media gallery value to load |
|
275
|
|
|
* @param string $storeId The store ID of the product media gallery value to load |
|
276
|
|
|
* @param string $entityId The entity ID of the parent product of the product media gallery value to load |
|
277
|
|
|
* |
|
278
|
|
|
* @return array The product media gallery value |
|
279
|
|
|
*/ |
|
280
|
|
|
public function loadProductMediaGalleryValue($valueId, $storeId, $entityId) |
|
281
|
|
|
{ |
|
282
|
|
|
$this->getProductProcessor()->loadProductMediaGalleryValue($valueId, $storeId, $entityId); |
|
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Persist's the passed product media gallery data and return's the ID. |
|
287
|
|
|
* |
|
288
|
|
|
* @param array $productMediaGallery The product media gallery data to persist |
|
289
|
|
|
* |
|
290
|
|
|
* @return string The ID of the persisted entity |
|
291
|
|
|
*/ |
|
292
|
|
|
public function persistProductMediaGallery($productMediaGallery) |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->getProductProcessor()->persistProductMediaGallery($productMediaGallery); |
|
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Persist's the passed product media gallery value data. |
|
299
|
|
|
* |
|
300
|
|
|
* @param array $productMediaGalleryValue The product media gallery value data to persist |
|
301
|
|
|
* |
|
302
|
|
|
* @return void |
|
303
|
|
|
*/ |
|
304
|
|
|
public function persistProductMediaGalleryValue($productMediaGalleryValue) |
|
305
|
|
|
{ |
|
306
|
|
|
$this->getProductProcessor()->persistProductMediaGalleryValue($productMediaGalleryValue); |
|
|
|
|
|
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Persist's the passed product media gallery value to entity data. |
|
311
|
|
|
* |
|
312
|
|
|
* @param array $productMediaGalleryValuetoEntity The product media gallery value to entity data to persist |
|
313
|
|
|
* |
|
314
|
|
|
* @return void |
|
315
|
|
|
*/ |
|
316
|
|
|
public function persistProductMediaGalleryValueToEntity($productMediaGalleryValuetoEntity) |
|
317
|
|
|
{ |
|
318
|
|
|
$this->getProductProcessor()->persistProductMediaGalleryValueToEntity($productMediaGalleryValuetoEntity); |
|
|
|
|
|
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Persist's the passed product media gallery value video data. |
|
323
|
|
|
* |
|
324
|
|
|
* @param array $productMediaGalleryValueVideo The product media gallery value video data to persist |
|
325
|
|
|
* |
|
326
|
|
|
* @return void |
|
327
|
|
|
*/ |
|
328
|
|
|
public function persistProductMediaGalleryValueVideo($productMediaGalleryValueVideo) |
|
329
|
|
|
{ |
|
330
|
|
|
$this->getProductProcessor()->persistProductMediaGalleryValueVideo($productMediaGalleryValueVideo); |
|
|
|
|
|
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: