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