|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\UrlRewrite\Observers\ProductUrlRewriteObserver |
|
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-url-rewrite |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\UrlRewrite\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
|
24
|
|
|
use TechDivision\Import\Product\UrlRewrite\Utils\ColumnKeys; |
|
25
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Observer that extracts the URL rewrite data to a specific CSV file. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link https://github.com/techdivision/import-product-url-rewrite |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
class ProductUrlRewriteObserver extends AbstractProductImportObserver |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The artefact type. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
const ARTEFACT_TYPE = 'url-rewrite'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The image artefacts that has to be exported. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $artefacts = array(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Process the observer's business logic. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array The processed row |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function process() |
|
59
|
|
|
{ |
|
60
|
|
|
|
|
61
|
|
|
// initialize the array for the artefacts and the store view codes |
|
62
|
|
|
$this->artefacts = array(); |
|
63
|
|
|
$storeViewCodes = array(); |
|
64
|
|
|
|
|
65
|
|
|
// load the SKU from the row |
|
66
|
|
|
$sku = $this->getValue(ColumnKeys::SKU); |
|
67
|
|
|
|
|
68
|
|
|
// prepare the store view code |
|
69
|
|
|
$this->getSubject()->prepareStoreViewCode(); |
|
70
|
|
|
|
|
71
|
|
|
// try to load the store view code |
|
72
|
|
|
$storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
|
73
|
|
|
|
|
74
|
|
|
// query whether or not we've a store view code |
|
75
|
|
|
if ($storeViewCode === StoreViewCodes::ADMIN) { |
|
76
|
|
|
// if not, load the websites the product is related with |
|
77
|
|
|
$websiteCodes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode')); |
|
78
|
|
|
|
|
79
|
|
|
// load the store view codes of all websites |
|
80
|
|
|
foreach ($websiteCodes as $websiteCode) { |
|
81
|
|
|
$storeViewCodes = array_merge($storeViewCodes, $this->getStoreViewCodesByWebsiteCode($websiteCode)); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
array_push($storeViewCodes, $storeViewCode); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// iterate over the available image fields |
|
88
|
|
|
foreach ($storeViewCodes as $storeViewCode) { |
|
89
|
|
|
// iterate over the store view codes and query if artefacts are already available |
|
90
|
|
|
if ($this->hasArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId = $this->getSubject()->getLastEntityId())) { |
|
|
|
|
|
|
91
|
|
|
// if yes, load the artefacs |
|
92
|
|
|
$this->artefacts = $this->getArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId); |
|
93
|
|
|
|
|
94
|
|
|
// override the existing data with the store view specific one |
|
95
|
|
|
for ($i = 0; $i < sizeof($this->artefacts); $i++) { |
|
|
|
|
|
|
96
|
|
|
// query whether or not a URL key has be specfied and the store view codes are equal |
|
97
|
|
|
if ($this->hasValue(ColumnKeys::URL_KEY) && $this->artefacts[$i][ColumnKeys::STORE_VIEW_CODE] === $storeViewCode) { |
|
98
|
|
|
// update the URL key |
|
99
|
|
|
$this->artefacts[$i][ColumnKeys::URL_KEY] = $this->getValue(ColumnKeys::URL_KEY); |
|
100
|
|
|
// update the visibility, if available |
|
101
|
|
|
if ($this->hasValue(ColumnKeys::VISIBILITY)) { |
|
102
|
|
|
$this->artefacts[$i][ColumnKeys::VISIBILITY] = $this->getValue(ColumnKeys::VISIBILITY); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// also update filename and line number |
|
106
|
|
|
$this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_FILENAME] = $this->getSubject()->getFilename(); |
|
107
|
|
|
$this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getSubject()->getLineNumber(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} else { |
|
111
|
|
|
// if no arefacts are available, append new data |
|
112
|
|
|
$artefact = $this->newArtefact( |
|
113
|
|
|
array( |
|
114
|
|
|
ColumnKeys::SKU => $sku, |
|
115
|
|
|
ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
|
116
|
|
|
ColumnKeys::CATEGORIES => $this->getValue(ColumnKeys::CATEGORIES), |
|
117
|
|
|
ColumnKeys::PRODUCT_WEBSITES => $this->getValue(ColumnKeys::PRODUCT_WEBSITES), |
|
118
|
|
|
ColumnKeys::VISIBILITY => $this->getValue(ColumnKeys::VISIBILITY), |
|
119
|
|
|
ColumnKeys::URL_KEY => $this->getValue(ColumnKeys::URL_KEY) |
|
120
|
|
|
), |
|
121
|
|
|
array( |
|
122
|
|
|
ColumnKeys::SKU => ColumnKeys::SKU, |
|
123
|
|
|
ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
|
124
|
|
|
ColumnKeys::CATEGORIES => ColumnKeys::CATEGORIES, |
|
125
|
|
|
ColumnKeys::PRODUCT_WEBSITES => ColumnKeys::PRODUCT_WEBSITES, |
|
126
|
|
|
ColumnKeys::VISIBILITY => ColumnKeys::VISIBILITY, |
|
127
|
|
|
ColumnKeys::URL_KEY => ColumnKeys::URL_KEY, |
|
128
|
|
|
) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
// append the base image to the artefacts |
|
132
|
|
|
$this->artefacts[] = $artefact; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// append the artefacts that has to be exported to the subject |
|
137
|
|
|
$this->addArtefacts($this->artefacts); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns an array with the codes of the store views related with the passed website code. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $websiteCode The code of the website to return the store view codes for |
|
144
|
|
|
* |
|
145
|
|
|
* @return array The array with the matching store view codes |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getStoreViewCodesByWebsiteCode($websiteCode) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->getSubject()->getStoreViewCodesByWebsiteCode($websiteCode); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Queries whether or not artefacts for the passed type and entity ID are available. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
156
|
|
|
* @param string $entityId The entity ID to return the artefacts for |
|
157
|
|
|
* |
|
158
|
|
|
* @return boolean TRUE if artefacts are available, else FALSE |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function hasArtefactsByTypeAndEntityId($type, $entityId) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getSubject()->hasArtefactsByTypeAndEntityId($type, $entityId); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return the artefacts for the passed type and entity ID. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $type The artefact type, e. g. configurable |
|
169
|
|
|
* @param string $entityId The entity ID to return the artefacts for |
|
170
|
|
|
* |
|
171
|
|
|
* @return array The array with the artefacts |
|
172
|
|
|
* @throws \Exception Is thrown, if no artefacts are available |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getArtefactsByTypeAndEntityId($type, $entityId) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->getSubject()->getArtefactsByTypeAndEntityId($type, $entityId); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Create's and return's a new empty artefact entity. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $columns The array with the column data |
|
183
|
|
|
* @param array $originalColumnNames The array with a mapping from the old to the new column names |
|
184
|
|
|
* |
|
185
|
|
|
* @return array The new artefact entity |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function newArtefact(array $columns, array $originalColumnNames) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->getSubject()->newArtefact($columns, $originalColumnNames); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Add the passed product type artefacts to the product with the |
|
194
|
|
|
* last entity ID. |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $artefacts The product type artefacts |
|
197
|
|
|
* |
|
198
|
|
|
* @return void |
|
199
|
|
|
* @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function addArtefacts(array $artefacts) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->getSubject()->addArtefacts(ProductUrlRewriteObserver::ARTEFACT_TYPE, $artefacts); |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
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: