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
|
|
|
* The image artefacts that has to be exported. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $artefacts = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Process the observer's business logic. |
61
|
|
|
* |
62
|
|
|
* @return array The processed row |
63
|
|
|
*/ |
64
|
|
|
protected function process() |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
// initialize the array for the artefacts |
68
|
|
|
$this->artefacts = array(); |
69
|
|
|
|
70
|
|
|
// process the images/additional images |
71
|
|
|
$this->processImages(); |
72
|
|
|
$this->processAdditionalImages(); |
73
|
|
|
|
74
|
|
|
// append the artefacts that has to be exported to the subject |
75
|
|
|
$this->addArtefacts($this->artefacts); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parses the column and exports the image data to a separate file. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function processImages() |
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
// load the store view code |
87
|
|
|
$storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE); |
88
|
|
|
$attributeSetCode = $this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE); |
89
|
|
|
|
90
|
|
|
// load the parent SKU from the row |
91
|
|
|
$parentSku = $this->getValue(ColumnKeys::SKU); |
92
|
|
|
|
93
|
|
|
// iterate over the available image fields |
94
|
|
|
foreach ($this->getImageTypes() as $imageColumnName => $labelColumnName) { |
95
|
|
|
// query whether or not, we've a base image |
96
|
|
|
if ($image = $this->getValue($imageColumnName)) { |
97
|
|
|
// initialize the label text |
98
|
|
|
$labelText = $this->getDefaultImageLabel(); |
99
|
|
|
|
100
|
|
|
// query whether or not a custom label text has been passed |
101
|
|
|
if ($this->hasValue($labelColumnName)) { |
102
|
|
|
$this->getValue($labelColumnName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// prepare the new base image |
106
|
|
|
$artefact = $this->newArtefact( |
107
|
|
|
array( |
108
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
109
|
|
|
ColumnKeys::ATTRIBUTE_SET_CODE => $attributeSetCode, |
110
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => $parentSku, |
111
|
|
|
ColumnKeys::IMAGE_PATH => $image, |
112
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $image, |
113
|
|
|
ColumnKeys::IMAGE_LABEL => $labelText |
114
|
|
|
), |
115
|
|
|
array( |
116
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
117
|
|
|
ColumnKeys::ATTRIBUTE_SET_CODE => ColumnKeys::ATTRIBUTE_SET_CODE, |
118
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
119
|
|
|
ColumnKeys::IMAGE_PATH => $imageColumnName, |
120
|
|
|
ColumnKeys::IMAGE_PATH_NEW => $imageColumnName, |
121
|
|
|
ColumnKeys::IMAGE_LABEL => $labelColumnName |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// append the base image to the artefacts |
126
|
|
|
$this->artefacts[] = $artefact; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Parses the column and exports the additional image data to a separate file. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function processAdditionalImages() |
137
|
|
|
{ |
138
|
|
|
|
139
|
|
|
// load the store view code |
140
|
|
|
$storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE); |
141
|
|
|
$attributeSetCode = $this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE); |
142
|
|
|
|
143
|
|
|
// load the parent SKU from the row |
144
|
|
|
$parentSku = $this->getValue(ColumnKeys::SKU); |
145
|
|
|
|
146
|
|
|
// query whether or not, we've additional images |
147
|
|
|
if ($additionalImages = $this->getValue(ColumnKeys::ADDITIONAL_IMAGES, null, array($this, 'explode'))) { |
148
|
|
|
// expand the additional image labels, if available |
149
|
|
|
$additionalImageLabels = $this->getValue(ColumnKeys::ADDITIONAL_IMAGE_LABELS, array(), array($this, 'explode')); |
150
|
|
|
|
151
|
|
|
// initialize the images with the found values |
152
|
|
|
foreach ($additionalImages as $key => $additionalImage) { |
153
|
|
|
// prepare the additional image |
154
|
|
|
$artefact = $this->newArtefact( |
155
|
|
|
array( |
156
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
157
|
|
|
ColumnKeys::ATTRIBUTE_SET_CODE => $attributeSetCode, |
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::ATTRIBUTE_SET_CODE => ColumnKeys::ATTRIBUTE_SET_CODE, |
168
|
|
|
ColumnKeys::IMAGE_PARENT_SKU => ColumnKeys::SKU, |
169
|
|
|
ColumnKeys::IMAGE_PATH => ColumnKeys::ADDITIONAL_IMAGES, |
170
|
|
|
ColumnKeys::IMAGE_PATH_NEW => ColumnKeys::ADDITIONAL_IMAGES, |
171
|
|
|
ColumnKeys::IMAGE_LABEL => ColumnKeys::ADDITIONAL_IMAGE_LABELS |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
// append the additional image to the artefacts |
176
|
|
|
$this->artefacts[] = $artefact; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return's the array with the available image types and their label columns. |
183
|
|
|
* |
184
|
|
|
* @return array The array with the available image types |
185
|
|
|
*/ |
186
|
|
|
protected function getImageTypes() |
187
|
|
|
{ |
188
|
|
|
return $this->getSubject()->getImageTypes(); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return's the default image label. |
193
|
|
|
* |
194
|
|
|
* @return string The default image label |
195
|
|
|
*/ |
196
|
|
|
protected function getDefaultImageLabel() |
197
|
|
|
{ |
198
|
|
|
return ProductMediaObserver::DEFAULT_IMAGE_LABEL; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Create's and return's a new empty artefact entity. |
203
|
|
|
* |
204
|
|
|
* @param array $columns The array with the column data |
205
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
206
|
|
|
* |
207
|
|
|
* @return array The new artefact entity |
208
|
|
|
*/ |
209
|
|
|
protected function newArtefact(array $columns, array $originalColumnNames) |
210
|
|
|
{ |
211
|
|
|
return $this->getSubject()->newArtefact($columns, $originalColumnNames); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Add the passed product type artefacts to the product with the |
216
|
|
|
* last entity ID. |
217
|
|
|
* |
218
|
|
|
* @param array $artefacts The product type artefacts |
219
|
|
|
* |
220
|
|
|
* @return void |
221
|
|
|
* @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() |
222
|
|
|
*/ |
223
|
|
|
protected function addArtefacts(array $artefacts) |
224
|
|
|
{ |
225
|
|
|
$this->getSubject()->addArtefacts(ProductMediaObserver::ARTEFACT_TYPE, $artefacts); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: