|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Observers\UrlRewriteUpdateObserver |
|
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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\CoreConfigDataKeys; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that creates/updates the product's URL rewrites. |
|
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 |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class UrlRewriteUpdateObserver extends UrlRewriteObserver |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Array with the existing URL rewrites of the actual product. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $existingUrlRewrites = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return's the URL rewrite for the passed request path. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $requestPath The request path to return the URL rewrite for |
|
49
|
|
|
* |
|
50
|
|
|
* @return array|null The URL rewrite |
|
51
|
|
|
*/ |
|
52
|
1 |
|
protected function getExistingUrlRewrite($requestPath) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (isset($this->existingUrlRewrites[$requestPath])) { |
|
55
|
|
|
return $this->existingUrlRewrites[$requestPath]; |
|
56
|
|
|
} |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Remove's the passed URL rewrite from the existing one's. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $urlRewrite The URL rewrite to remove |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function removeExistingUrlRewrite(array $urlRewrite) |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
// load request path |
|
70
|
|
|
$requestPath = $urlRewrite[MemberNames::REQUEST_PATH]; |
|
71
|
|
|
|
|
72
|
|
|
// query whether or not the URL rewrite exists and remove it, if available |
|
73
|
|
|
if (isset($this->existingUrlRewrites[$requestPath])) { |
|
74
|
|
|
unset($this->existingUrlRewrites[$requestPath]); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Process the observer's business logic. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
* @see \TechDivision\Import\Product\Observers\UrlRewriteObserver::process() |
|
83
|
|
|
*/ |
|
84
|
1 |
|
protected function process() |
|
85
|
|
|
{ |
|
86
|
|
|
|
|
87
|
|
|
// process the new URL rewrites first |
|
88
|
1 |
|
parent::process(); |
|
89
|
|
|
|
|
90
|
|
|
// query whether or not 301 redirects have to be created |
|
91
|
1 |
|
if ($this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_SAVE_REWRITES_HISTORY, true)) { |
|
92
|
|
|
// load the root category |
|
93
|
1 |
|
$rootCategory = $this->getRootCategory(); |
|
94
|
|
|
|
|
95
|
|
|
// create redirect URL rewrites for the existing URL rewrites |
|
96
|
1 |
|
foreach ($this->existingUrlRewrites as $existingUrlRewrite) { |
|
97
|
|
|
// if the URL rewrite has been created manually |
|
98
|
1 |
|
if ((integer) $existingUrlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
|
99
|
|
|
// do NOT create another redirect |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// if the URL rewrite already IS a redirect |
|
104
|
1 |
|
if ((integer) $existingUrlRewrite[MemberNames::REDIRECT_TYPE] !== 0) { |
|
105
|
|
|
// do NOT create another redirect |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// load the metadata from the existing URL rewrite |
|
110
|
1 |
|
$metadata = $this->getMetadata($existingUrlRewrite); |
|
111
|
|
|
|
|
112
|
|
|
// initialize the category with the root category |
|
113
|
1 |
|
$category = $rootCategory; |
|
114
|
|
|
|
|
115
|
|
|
// query whether or not, the existing URL rewrite has been replaced |
|
116
|
1 |
|
if (isset($this->urlRewrites[$metadata['category_id']])) { |
|
117
|
|
|
// if yes, load the category of the original one |
|
118
|
1 |
|
$category = $this->getCategory($metadata['category_id']); |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
// load target path/metadata for the actual category |
|
122
|
1 |
|
$targetPath = $this->prepareRequestPath($category); |
|
123
|
1 |
|
$metadata = serialize($this->prepareMetadata($category)); |
|
124
|
|
|
|
|
125
|
|
|
// override data with the 301 configuration |
|
126
|
|
|
$attr = array( |
|
127
|
1 |
|
MemberNames::IS_AUTOGENERATED => 0, |
|
128
|
1 |
|
MemberNames::REDIRECT_TYPE => 301, |
|
129
|
1 |
|
MemberNames::METADATA => $metadata, |
|
130
|
1 |
|
MemberNames::TARGET_PATH => $targetPath, |
|
131
|
1 |
|
); |
|
132
|
|
|
|
|
133
|
|
|
// merge and return the prepared URL rewrite |
|
134
|
1 |
|
$existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr); |
|
135
|
|
|
|
|
136
|
|
|
// create the URL rewrite |
|
137
|
1 |
|
$this->persistUrlRewrite($existingUrlRewrite); |
|
138
|
1 |
|
} |
|
139
|
1 |
|
} |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Prepare's the URL rewrites that has to be created/updated. |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
* @see \TechDivision\Import\Product\Observers\UrlRewriteObserver::prepareUrlRewrites() |
|
147
|
|
|
*/ |
|
148
|
1 |
|
protected function prepareUrlRewrites() |
|
149
|
|
|
{ |
|
150
|
|
|
|
|
151
|
|
|
// (re-)initialize the array for the existing URL rewrites |
|
152
|
1 |
|
$this->existingUrlRewrites = array(); |
|
153
|
|
|
|
|
154
|
|
|
// prepare the new URL rewrites first |
|
155
|
1 |
|
parent::prepareUrlRewrites(); |
|
156
|
|
|
|
|
157
|
|
|
// load the existing URL rewrites of the actual entity |
|
158
|
1 |
|
$existingUrlRewrites = $this->getUrlRewritesByEntityTypeAndEntityIdAndStoreId( |
|
159
|
1 |
|
UrlRewriteObserver::ENTITY_TYPE, |
|
160
|
1 |
|
$this->getSubject()->getLastEntityId(), |
|
|
|
|
|
|
161
|
1 |
|
$this->getSubject()->getRowStoreId() |
|
|
|
|
|
|
162
|
1 |
|
); |
|
163
|
|
|
|
|
164
|
|
|
// prepare the existing URL rewrites to improve searching them by request path |
|
165
|
1 |
|
foreach ($existingUrlRewrites as $existingUrlRewrite) { |
|
166
|
1 |
|
$this->existingUrlRewrites[$existingUrlRewrite[MemberNames::REQUEST_PATH]] = $existingUrlRewrite; |
|
167
|
1 |
|
} |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Initialize the category product with the passed attributes and returns an instance. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $attr The category product attributes |
|
174
|
|
|
* |
|
175
|
|
|
* @return array The initialized category product |
|
176
|
|
|
*/ |
|
177
|
1 |
|
protected function initializeUrlRewrite(array $attr) |
|
178
|
|
|
{ |
|
179
|
|
|
|
|
180
|
|
|
// try to load the URL rewrite for the store ID and request path |
|
181
|
1 |
|
if ($urlRewrite = $this->getExistingUrlRewrite($attr[MemberNames::REQUEST_PATH])) { |
|
182
|
|
|
// if a URL rewrite has been found, do NOT create a redirect |
|
183
|
|
|
$this->removeExistingUrlRewrite($urlRewrite); |
|
184
|
|
|
|
|
185
|
|
|
// if the found URL rewrite has been created manually |
|
186
|
|
|
if ((integer) $urlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
|
187
|
|
|
// do NOT update it nor create a another redirect |
|
188
|
|
|
return false; |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// if the found URL rewrite has been autogenerated, then update it |
|
192
|
|
|
return $this->mergeEntity($urlRewrite, $attr); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// simple return the attributes |
|
196
|
1 |
|
return $attr; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Initialize the URL rewrite product => category relation with the passed attributes |
|
201
|
|
|
* and returns an instance. |
|
202
|
|
|
* |
|
203
|
|
|
* @param array $attr The URL rewrite product => category relation attributes |
|
204
|
|
|
* |
|
205
|
|
|
* @return array|null The initialized URL rewrite product => category relation |
|
206
|
|
|
*/ |
|
207
|
1 |
|
protected function initializeUrlRewriteProductCategory($attr) |
|
208
|
|
|
{ |
|
209
|
|
|
|
|
210
|
|
|
// try to load the URL rewrite product category relation |
|
211
|
1 |
|
if ($urlRewriteProductCategory = $this->loadUrlRewriteProductCategory($attr[MemberNames::URL_REWRITE_ID])) { |
|
212
|
|
|
return $this->mergeEntity($urlRewriteProductCategory, $attr); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// simple return the URL rewrite product category |
|
216
|
1 |
|
return $attr; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Return's the unserialized metadata of the passed URL rewrite. If the |
|
221
|
|
|
* metadata doesn't contain a category ID, the category ID of the root |
|
222
|
|
|
* category will be added. |
|
223
|
|
|
* |
|
224
|
|
|
* @param array $urlRewrite The URL rewrite to return the metadata for |
|
225
|
|
|
* |
|
226
|
|
|
* @return array The metadata of the passed URL rewrite |
|
227
|
|
|
*/ |
|
228
|
1 |
|
protected function getMetadata($urlRewrite) |
|
229
|
|
|
{ |
|
230
|
|
|
|
|
231
|
|
|
// initialize the array with the metaddata |
|
232
|
1 |
|
$metadata = array(); |
|
233
|
|
|
|
|
234
|
|
|
// try to unserialize the metadata from the passed URL rewrite |
|
235
|
1 |
|
if (isset($urlRewrite[MemberNames::METADATA])) { |
|
236
|
1 |
|
$metadata = unserialize($urlRewrite[MemberNames::METADATA]); |
|
237
|
1 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
// query whether or not a category ID has been found |
|
240
|
1 |
|
if (isset($metadata['category_id'])) { |
|
241
|
|
|
// if yes, return the metadata |
|
242
|
1 |
|
return $metadata; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// if not, append the ID of the root category |
|
246
|
1 |
|
$rootCategory = $this->getRootCategory(); |
|
247
|
1 |
|
$metadata['category_id'] = $rootCategory[MemberNames::ENTITY_ID]; |
|
248
|
|
|
|
|
249
|
|
|
// and return the metadata |
|
250
|
1 |
|
return $metadata; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Return's the category with the passed ID. |
|
255
|
|
|
* |
|
256
|
|
|
* @param integer $categoryId The ID of the category to return |
|
257
|
|
|
* |
|
258
|
|
|
* @return array The category data |
|
259
|
|
|
*/ |
|
260
|
1 |
|
protected function getCategory($categoryId) |
|
261
|
|
|
{ |
|
262
|
1 |
|
return $this->getSubject()->getCategory($categoryId); |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Return's the URL rewrites for the passed URL entity type and ID. |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $entityType The entity type to load the URL rewrites for |
|
269
|
|
|
* @param integer $entityId The entity ID to load the URL rewrites for |
|
270
|
|
|
* @param integer $storeId The store ID to load the URL rewrites for |
|
271
|
|
|
* |
|
272
|
|
|
* @return array The URL rewrites |
|
273
|
|
|
*/ |
|
274
|
1 |
|
public function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
|
275
|
|
|
{ |
|
276
|
1 |
|
return $this->getProductBunchProcessor()->getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Return's the URL rewrite product category relation for the passed |
|
281
|
|
|
* URL rewrite ID. |
|
282
|
|
|
* |
|
283
|
|
|
* @param integer $urlRewriteId The URL rewrite ID to load the URL rewrite product category relation for |
|
284
|
|
|
* |
|
285
|
|
|
* @return array|false The URL rewrite product category relations |
|
286
|
|
|
*/ |
|
287
|
1 |
|
protected function loadUrlRewriteProductCategory($urlRewriteId) |
|
288
|
|
|
{ |
|
289
|
1 |
|
return $this->getProductBunchProcessor()->loadUrlRewriteProductCategory($urlRewriteId); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
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: