|
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 TechDivision\Import\Utils\RegistryKeys; |
|
24
|
|
|
use TechDivision\Import\Subjects\FileUploadTrait; |
|
25
|
|
|
use TechDivision\Import\Subjects\FileUploadSubjectInterface; |
|
26
|
|
|
use TechDivision\Import\Product\Subjects\AbstractProductSubject; |
|
27
|
|
|
use TechDivision\Import\Product\Media\Utils\ConfigurationKeys; |
|
28
|
|
|
use TechDivision\Import\Product\Media\Exceptions\MapSkuToEntityIdException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The subject implementation for the product media handling. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/techdivision/import-product-media |
|
37
|
|
|
* @link http://www.techdivision.com |
|
38
|
|
|
*/ |
|
39
|
|
|
class MediaSubject extends AbstractProductSubject implements FileUploadSubjectInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The trait that provides file upload functionality. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \TechDivision\Import\Subjects\FileUploadTrait |
|
46
|
|
|
*/ |
|
47
|
|
|
use FileUploadTrait; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The name of the craeted image. |
|
51
|
|
|
* |
|
52
|
|
|
* @var integer |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $parentImage; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The ID of the parent product to relate the variant with. |
|
58
|
|
|
* |
|
59
|
|
|
* @var integer |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $parentId; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The value ID of the created media gallery entry. |
|
65
|
|
|
* |
|
66
|
|
|
* @var integer |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $parentValueId; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The Magento installation directory. |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $installationDir; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The position counter, if no position for the product media gallery value has been specified. |
|
79
|
|
|
* |
|
80
|
|
|
* @var integer |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $positionCounter = 1; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The available stores. |
|
86
|
|
|
* |
|
87
|
|
|
* @var array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $stores = array(); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The mapping for the SKUs to the created entity IDs. |
|
93
|
|
|
* |
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $skuEntityIdMapping = array(); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Intializes the previously loaded global data for exactly one variants. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $serial The serial of the actual import |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setUp($serial) |
|
106
|
|
|
{ |
|
107
|
|
|
|
|
108
|
|
|
// invoke parent method |
|
109
|
|
|
parent::setUp($serial); |
|
110
|
|
|
|
|
111
|
|
|
// load the entity manager and the registry processor |
|
112
|
|
|
$registryProcessor = $this->getRegistryProcessor(); |
|
113
|
|
|
|
|
114
|
|
|
// load the status of the actual import process |
|
115
|
|
|
$status = $registryProcessor->getAttribute($serial); |
|
116
|
|
|
|
|
117
|
|
|
// load the attribute set we've prepared intially |
|
118
|
|
|
$this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING]; |
|
119
|
|
|
|
|
120
|
|
|
// initialize media directory => can be absolute or relative |
|
121
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
|
122
|
|
|
$this->setMediaDir( |
|
123
|
|
|
$this->resolvePath( |
|
124
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY) |
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// initialize images directory => can be absolute or relative |
|
130
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
|
131
|
|
|
$this->setImagesFileDir( |
|
132
|
|
|
$this->resolvePath( |
|
133
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY) |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set's the name of the created image. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $parentImage The name of the created image |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setParentImage($parentImage) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->parentImage = $parentImage; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return's the name of the created image. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string The name of the created image |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getParentImage() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->parentImage; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set's the ID of the parent product to relate the variant with. |
|
163
|
|
|
* |
|
164
|
|
|
* @param integer $parentId The ID of the parent product |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
public function setParentId($parentId) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->parentId = $parentId; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Return's the ID of the parent product to relate the variant with. |
|
175
|
|
|
* |
|
176
|
|
|
* @return integer The ID of the parent product |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getParentId() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->parentId; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set's the value ID of the created media gallery entry. |
|
185
|
|
|
* |
|
186
|
|
|
* @param integer $parentValueId The ID of the created media gallery entry |
|
187
|
|
|
* |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setParentValueId($parentValueId) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->parentValueId = $parentValueId; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Return's the value ID of the created media gallery entry. |
|
197
|
|
|
* |
|
198
|
|
|
* @return integer The ID of the created media gallery entry |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getParentValueId() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->parentValueId; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Reset the position counter to 1. |
|
207
|
|
|
* |
|
208
|
|
|
* @return void |
|
209
|
|
|
*/ |
|
210
|
|
|
public function resetPositionCounter() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->positionCounter = 1; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Returns the acutal value of the position counter and raise's it by one. |
|
217
|
|
|
* |
|
218
|
|
|
* @return integer The actual value of the position counter |
|
219
|
|
|
*/ |
|
220
|
|
|
public function raisePositionCounter() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->positionCounter++; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Return the entity ID for the passed SKU. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $sku The SKU to return the entity ID for |
|
229
|
|
|
* |
|
230
|
|
|
* @return integer The mapped entity ID |
|
231
|
|
|
* @throws \TechDivision\Import\Product\Media\Exceptions\MapSkuToEntityIdException Is thrown if the SKU is not mapped yet |
|
232
|
|
|
*/ |
|
233
|
|
|
public function mapSkuToEntityId($sku) |
|
234
|
|
|
{ |
|
235
|
|
|
|
|
236
|
|
|
// query weather or not the SKU has been mapped |
|
237
|
|
|
if (isset($this->skuEntityIdMapping[$sku])) { |
|
238
|
|
|
return $this->skuEntityIdMapping[$sku]; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// throw an exception if the SKU has not been mapped yet |
|
242
|
|
|
throw new MapSkuToEntityIdException(sprintf('Found not mapped entity ID for SKU %s', $sku)); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Return's the store for the passed store code. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $storeCode The store code to return the store for |
|
249
|
|
|
* |
|
250
|
|
|
* @return array The requested store |
|
251
|
|
|
* @throws \Exception Is thrown, if the requested store is not available |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getStoreByStoreCode($storeCode) |
|
254
|
|
|
{ |
|
255
|
|
|
|
|
256
|
|
|
// query whether or not the store with the passed store code exists |
|
257
|
|
|
if (isset($this->stores[$storeCode])) { |
|
258
|
|
|
return $this->stores[$storeCode]; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
// throw an exception, if not |
|
262
|
|
|
throw new \Exception(sprintf('Found invalid store code %s', $storeCode)); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.