1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Media\Observers\ProductMediaObserver |
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\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Media\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Observer that extracts theproduct's media data from a CSV file to be added to media specifi CSV file. |
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-product-media |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
class ProductMediaObserver extends AbstractProductImportObserver |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The artefact type. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
const ARTEFACT_TYPE = 'media'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The default image label. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const DEFAULT_IMAGE_LABEL = 'Image'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Process the observer's business logic. |
54
|
|
|
* |
55
|
|
|
* @return array The processed row |
56
|
|
|
*/ |
57
|
|
|
protected function process() |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
// initialize the array for the product media |
61
|
|
|
$artefacts = array(); |
62
|
|
|
|
63
|
|
|
// load the store view code |
64
|
|
|
$storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE); |
65
|
|
|
|
66
|
|
|
// load the parent SKU from the row |
67
|
|
|
$parentSku = $this->getValue(ColumnKeys::SKU); |
68
|
|
|
|
69
|
|
|
// query whether or not, we've a base image |
70
|
|
View Code Duplication |
if ($baseImage = $this->getValue(ColumnKeys::BASE_IMAGE)) { |
|
|
|
|
71
|
|
|
// prepare the new base image |
72
|
|
|
$artefact = $this->newArtefact( |
73
|
|
|
array( |
74
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
75
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => $parentSku, |
76
|
|
|
ColumnKeys::IMAGE_PATH => $baseImage, |
77
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $baseImage, |
78
|
|
|
ColumnKeys::IMAGE_LABEL => $this->hasValue(ColumnKeys::BASE_IMAGE_LABEL) ? |
79
|
|
|
$this->getValue(ColumnKeys::BASE_IMAGE_LABEL) : |
80
|
|
|
$this->getDefaultImageLabel() |
81
|
|
|
), |
82
|
|
|
array( |
83
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
84
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
85
|
|
|
ColumnKeys::IMAGE_PATH => ColumnKeys::BASE_IMAGE, |
86
|
|
|
ColumnKeys::IMAGE_PATH_NEW => ColumnKeys::BASE_IMAGE, |
87
|
|
|
ColumnKeys::IMAGE_LABEL => ColumnKeys::BASE_IMAGE_LABEL |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
// append the base image to the artefacts |
92
|
|
|
$artefacts[] = $artefact; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// query whether or not, we've a small image |
96
|
|
View Code Duplication |
if ($smallImage = $this->getValue(ColumnKeys::SMALL_IMAGE)) { |
|
|
|
|
97
|
|
|
// prepare the small image |
98
|
|
|
$artefact = $this->newArtefact( |
99
|
|
|
array( |
100
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
101
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => $parentSku, |
102
|
|
|
ColumnKeys::IMAGE_PATH => $smallImage, |
103
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $smallImage, |
104
|
|
|
ColumnKeys::IMAGE_LABEL => $this->hasValue(ColumnKeys::SMALL_IMAGE_LABEL) ? |
105
|
|
|
$this->getValue(ColumnKeys::SMALL_IMAGE_LABEL) : |
106
|
|
|
$this->getDefaultImageLabel() |
107
|
|
|
), |
108
|
|
|
array( |
109
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
110
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
111
|
|
|
ColumnKeys::IMAGE_PATH => ColumnKeys::SMALL_IMAGE, |
112
|
|
|
ColumnKeys::IMAGE_PATH_NEW => ColumnKeys::SMALL_IMAGE, |
113
|
|
|
ColumnKeys::IMAGE_LABEL => ColumnKeys::SMALL_IMAGE_LABEL |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
// append the small image to the artefacts |
118
|
|
|
$artefacts[] = $artefact; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// query whether or not, we've a small thumbnail |
122
|
|
View Code Duplication |
if ($thumbnailImage = $this->getValue(ColumnKeys::THUMBNAIL_IMAGE)) { |
|
|
|
|
123
|
|
|
// prepare the thumbnail image |
124
|
|
|
$artefact = $this->newArtefact( |
125
|
|
|
array( |
126
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
127
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => $parentSku, |
128
|
|
|
ColumnKeys::IMAGE_PATH => $thumbnailImage, |
129
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $thumbnailImage, |
130
|
|
|
ColumnKeys::IMAGE_LABEL => $this->hasValue(ColumnKeys::THUMBNAIL_IMAGE_LABEL) ? |
131
|
|
|
$this->getValue(ColumnKeys::THUMBNAIL_IMAGE_LABEL) : |
132
|
|
|
$this->getDefaultImageLabel() |
133
|
|
|
), |
134
|
|
|
array( |
135
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
136
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
137
|
|
|
ColumnKeys::IMAGE_PATH => ColumnKeys::THUMBNAIL_IMAGE, |
138
|
|
|
ColumnKeys::IMAGE_PATH_NEW => ColumnKeys::THUMBNAIL_IMAGE, |
139
|
|
|
ColumnKeys::IMAGE_LABEL => ColumnKeys::THUMBNAIL_IMAGE_LABEL |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
// append the thumbnail image to the artefacts |
144
|
|
|
$artefacts[] = $artefact; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// query whether or not, we've additional images |
148
|
|
|
if ($additionalImages = $this->getValue(ColumnKeys::ADDITIONAL_IMAGES, null, array($this, 'explode'))) { |
149
|
|
|
// expand the additional image labels, if available |
150
|
|
|
$additionalImageLabels = $this->getValue(ColumnKeys::ADDITIONAL_IMAGE_LABELS, array(), array($this, 'explode')); |
151
|
|
|
|
152
|
|
|
// initialize the images with the found values |
153
|
|
|
foreach ($additionalImages as $key => $additionalImage) { |
154
|
|
|
// prepare the additional image |
155
|
|
|
$artefact = $this->newArtefact( |
156
|
|
|
array( |
157
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
158
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => $parentSku, |
159
|
|
|
ColumnKeys::IMAGE_PATH => $additionalImage, |
160
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $additionalImage, |
161
|
|
|
ColumnKeys::IMAGE_LABEL => isset($additionalImageLabels[$key]) ? |
162
|
|
|
$additionalImageLabels[$key] : |
163
|
|
|
$this->getDefaultImageLabel() |
164
|
|
|
), |
165
|
|
|
array( |
166
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
167
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
168
|
|
|
ColumnKeys::IMAGE_PATH => ColumnKeys::ADDITIONAL_IMAGES, |
169
|
|
|
ColumnKeys::IMAGE_PATH_NEW => ColumnKeys::ADDITIONAL_IMAGES, |
170
|
|
|
ColumnKeys::IMAGE_LABEL => ColumnKeys::ADDITIONAL_IMAGE_LABELS |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
// append the additional image to the artefacts |
175
|
|
|
$artefacts[] = $artefact; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// append the images to the subject |
180
|
|
|
$this->addArtefacts($artefacts); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return's the default image label. |
185
|
|
|
* |
186
|
|
|
* @return string The default image label |
187
|
|
|
*/ |
188
|
|
|
protected function getDefaultImageLabel() |
189
|
|
|
{ |
190
|
|
|
return ProductMediaObserver::DEFAULT_IMAGE_LABEL; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Create's and return's a new empty artefact entity. |
195
|
|
|
* |
196
|
|
|
* @param array $columns The array with the column data |
197
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
198
|
|
|
* |
199
|
|
|
* @return array The new artefact entity |
200
|
|
|
*/ |
201
|
|
|
protected function newArtefact(array $columns, array $originalColumnNames) |
202
|
|
|
{ |
203
|
|
|
return $this->getSubject()->newArtefact($columns, $originalColumnNames); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Add the passed product type artefacts to the product with the |
208
|
|
|
* last entity ID. |
209
|
|
|
* |
210
|
|
|
* @param array $artefacts The product type artefacts |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
* @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() |
214
|
|
|
*/ |
215
|
|
|
protected function addArtefacts(array $artefacts) |
216
|
|
|
{ |
217
|
|
|
$this->getSubject()->addArtefacts(ProductMediaObserver::ARTEFACT_TYPE, $artefacts); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.