1 | <?php |
||
37 | class UrlRewriteUpdateObserver extends UrlRewriteObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Array with the existing URL rewrites of the actual product. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $existingUrlRewrites = array(); |
||
46 | |||
47 | /** |
||
48 | * Process the observer's business logic. |
||
49 | * |
||
50 | * @return void |
||
51 | * @see \TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver::process() |
||
52 | */ |
||
53 | 1 | protected function process() |
|
54 | { |
||
55 | |||
56 | // process the new URL rewrites first |
||
57 | 1 | parent::process(); |
|
58 | |||
59 | // load the root category |
||
60 | 1 | $rootCategory = $this->getRootCategory(); |
|
61 | |||
62 | // create redirect URL rewrites for the existing URL rewrites |
||
63 | 1 | foreach ($this->existingUrlRewrites as $existingUrlRewrite) { |
|
64 | // if the URL rewrite has been created manually |
||
65 | 1 | if ((integer) $existingUrlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
|
66 | // do NOTHING, because someone really WANTED to create THIS redirect |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | // query whether or not 301 redirects have to be created, so don't create redirects |
||
71 | // if the product is NOT visible or the rewrite history has been deactivated |
||
72 | 1 | if ($this->isVisible() && $this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_SAVE_REWRITES_HISTORY, true)) { |
|
73 | // if the URL rewrite already IS a redirect |
||
74 | 1 | if ((integer) $existingUrlRewrite[MemberNames::REDIRECT_TYPE] !== 0) { |
|
75 | // do NOT create another redirect or update the actual one |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | // initialize the data with the URL rewrites new 301 configuration |
||
80 | 1 | $attr = array(MemberNames::REDIRECT_TYPE => 301); |
|
81 | |||
82 | // initialize the category with the root category |
||
83 | 1 | $category = $rootCategory; |
|
84 | |||
85 | // load the metadata from the existing URL rewrite |
||
86 | 1 | $metadata = $this->getMetadata($existingUrlRewrite); |
|
87 | |||
88 | // query whether or not the URL key of the existing URL rewrite has changed |
||
89 | 1 | if (isset($this->urlRewrites[$metadata[UrlRewriteObserver::CATEGORY_ID]])) { |
|
90 | try { |
||
91 | // if yes, try to load the original category and OVERRIDE the default category |
||
92 | 1 | $category = $this->getCategory($metadata[UrlRewriteObserver::CATEGORY_ID]); |
|
93 | 1 | } catch (\Exception $e) { |
|
94 | // if the old category can NOT be loaded, override the metadata |
||
95 | // of the category with the data of the default category |
||
96 | $attr[MemberNames::METADATA] = serialize($this->prepareMetadata($category)); |
||
97 | |||
98 | // finally log a warning that the old category is not available ony more |
||
99 | $this->getSubject() |
||
100 | ->getSystemLogger() |
||
101 | ->warning(sprintf('Category with ID %d is not longer available', $metadata[UrlRewriteObserver::CATEGORY_ID])); |
||
102 | } |
||
103 | 1 | } |
|
104 | |||
105 | // load target path/metadata for the actual category |
||
106 | 1 | $attr[MemberNames::TARGET_PATH] = $this->prepareRequestPath($category); |
|
107 | |||
108 | // merge and return the prepared URL rewrite |
||
109 | 1 | $existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr); |
|
110 | |||
111 | // create the URL rewrite |
||
112 | 1 | $this->persistUrlRewrite($existingUrlRewrite); |
|
113 | |||
114 | 1 | } else { |
|
115 | // query whether or not the URL rewrite has to be removed |
||
116 | if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) && |
||
117 | $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) |
||
118 | ) { |
||
119 | // delete the existing URL rewrite |
||
120 | $this->deleteUrlRewrite($existingUrlRewrite); |
||
121 | |||
122 | // log a message, that old URL rewrites have been cleaned-up |
||
123 | $this->getSubject() |
||
124 | ->getSystemLogger() |
||
125 | ->notice( |
||
126 | sprintf( |
||
127 | 'Cleaned-up %d URL rewrite "%s" for product with SKU "%s"', |
||
128 | $existingUrlRewrite[MemberNames::REQUEST_PATH], |
||
129 | $this->getValue(ColumnKeys::SKU) |
||
130 | ) |
||
131 | ); |
||
132 | } |
||
133 | } |
||
134 | 1 | } |
|
135 | 1 | } |
|
136 | |||
137 | /** |
||
138 | * Return's the URL rewrite for the passed request path. |
||
139 | * |
||
140 | * @param string $requestPath The request path to return the URL rewrite for |
||
141 | * |
||
142 | * @return array|null The URL rewrite |
||
143 | */ |
||
144 | protected function getExistingUrlRewrite($requestPath) |
||
150 | |||
151 | /** |
||
152 | * Remove's the passed URL rewrite from the existing one's. |
||
153 | * |
||
154 | * @param array $urlRewrite The URL rewrite to remove |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | protected function removeExistingUrlRewrite(array $urlRewrite) |
||
159 | { |
||
160 | |||
161 | // load request path |
||
162 | $requestPath = $urlRewrite[MemberNames::REQUEST_PATH]; |
||
163 | |||
164 | // query whether or not the URL rewrite exists and remove it, if available |
||
165 | if (isset($this->existingUrlRewrites[$requestPath])) { |
||
166 | unset($this->existingUrlRewrites[$requestPath]); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Prepare's the URL rewrites that has to be created/updated. |
||
172 | * |
||
173 | * @return void |
||
174 | * @see \TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver::prepareUrlRewrites() |
||
175 | */ |
||
176 | 1 | protected function prepareUrlRewrites() |
|
177 | { |
||
178 | |||
179 | // (re-)initialize the array for the existing URL rewrites |
||
180 | 1 | $this->existingUrlRewrites = array(); |
|
181 | |||
182 | // prepare the new URL rewrites first |
||
183 | 1 | parent::prepareUrlRewrites(); |
|
184 | |||
185 | // load the store ID to use |
||
186 | 1 | $storeId = $this->getSubject()->getRowStoreId(); |
|
|
|||
187 | |||
188 | // load the existing URL rewrites of the actual entity |
||
189 | 1 | $existingUrlRewrites = $this->getUrlRewritesByEntityTypeAndEntityIdAndStoreId( |
|
190 | 1 | UrlRewriteObserver::ENTITY_TYPE, |
|
191 | 1 | $this->entityId, |
|
192 | $storeId |
||
193 | 1 | ); |
|
194 | |||
195 | // prepare the existing URL rewrites to improve searching them by request path |
||
196 | 1 | foreach ($existingUrlRewrites as $existingUrlRewrite) { |
|
197 | 1 | $this->existingUrlRewrites[$existingUrlRewrite[MemberNames::REQUEST_PATH]] = $existingUrlRewrite; |
|
198 | 1 | } |
|
199 | 1 | } |
|
200 | |||
201 | /** |
||
202 | * Initialize the category product with the passed attributes and returns an instance. |
||
203 | * |
||
204 | * @param array $attr The category product attributes |
||
205 | * |
||
206 | * @return array The initialized category product |
||
207 | */ |
||
208 | 1 | protected function initializeUrlRewrite(array $attr) |
|
209 | { |
||
210 | |||
211 | // load the category ID of the passed URL rewrite entity |
||
212 | 1 | $categoryId = $this->getCategoryIdFromMetadata($attr); |
|
213 | |||
214 | // iterate over the availabel URL rewrites to find the one that matches the category ID |
||
215 | 1 | foreach ($this->existingUrlRewrites as $urlRewrite) { |
|
216 | // compare the category IDs AND the request path |
||
217 | 1 | if ($categoryId === $this->getCategoryIdFromMetadata($urlRewrite) && |
|
218 | 1 | $attr[MemberNames::REQUEST_PATH] === $urlRewrite[MemberNames::REQUEST_PATH] |
|
219 | 1 | ) { |
|
220 | // if a URL rewrite has been found, do NOT create a redirect |
||
221 | $this->removeExistingUrlRewrite($urlRewrite); |
||
222 | |||
223 | // if the found URL rewrite has been created manually |
||
224 | if ((integer) $urlRewrite[MemberNames::IS_AUTOGENERATED] === 0) { |
||
225 | // do NOT update it nor create another redirect |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | // if the found URL rewrite has been autogenerated, then update it |
||
230 | return $this->mergeEntity($urlRewrite, $attr); |
||
231 | } |
||
232 | 1 | } |
|
233 | |||
234 | // simple return the attributes |
||
235 | 1 | return $attr; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * Extracts the category ID of the passed URL rewrite entity, if available, and return's it. |
||
240 | * |
||
241 | * @param array $attr The URL rewrite entity to extract and return the category ID for |
||
242 | * |
||
243 | * @return integer|null The category ID if available, else NULL |
||
244 | */ |
||
245 | 1 | protected function getCategoryIdFromMetadata(array $attr) |
|
254 | |||
255 | /** |
||
256 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
257 | * and returns an instance. |
||
258 | * |
||
259 | * @param array $attr The URL rewrite product => category relation attributes |
||
260 | * |
||
261 | * @return array|null The initialized URL rewrite product => category relation |
||
262 | */ |
||
263 | 1 | protected function initializeUrlRewriteProductCategory($attr) |
|
274 | |||
275 | /** |
||
276 | * Return's the unserialized metadata of the passed URL rewrite. If the |
||
277 | * metadata doesn't contain a category ID, the category ID of the root |
||
278 | * category will be added. |
||
279 | * |
||
280 | * @param array $urlRewrite The URL rewrite to return the metadata for |
||
281 | * |
||
282 | * @return array The metadata of the passed URL rewrite |
||
283 | */ |
||
284 | 1 | protected function getMetadata($urlRewrite) |
|
285 | { |
||
286 | |||
287 | // initialize the array with the metaddata |
||
288 | 1 | $metadata = array(); |
|
289 | |||
290 | // try to unserialize the metadata from the passed URL rewrite |
||
291 | 1 | if (isset($urlRewrite[MemberNames::METADATA])) { |
|
292 | 1 | $metadata = unserialize($urlRewrite[MemberNames::METADATA]); |
|
293 | 1 | } |
|
294 | |||
295 | // query whether or not a category ID has been found |
||
296 | 1 | if (isset($metadata[UrlRewriteObserver::CATEGORY_ID])) { |
|
297 | // if yes, return the metadata |
||
298 | 1 | return $metadata; |
|
299 | } |
||
300 | |||
301 | // if not, append the ID of the root category |
||
302 | 1 | $rootCategory = $this->getRootCategory(); |
|
303 | 1 | $metadata[UrlRewriteObserver::CATEGORY_ID] = $rootCategory[MemberNames::ENTITY_ID]; |
|
304 | |||
305 | // and return the metadata |
||
306 | 1 | return $metadata; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Return's the category with the passed ID. |
||
311 | * |
||
312 | * @param integer $categoryId The ID of the category to return |
||
313 | * |
||
314 | * @return array The category data |
||
315 | */ |
||
316 | 1 | protected function getCategory($categoryId) |
|
320 | |||
321 | /** |
||
322 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
323 | * |
||
324 | * @param string $entityType The entity type to load the URL rewrites for |
||
325 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
326 | * @param integer $storeId The store ID to load the URL rewrites for |
||
327 | * |
||
328 | * @return array The URL rewrites |
||
329 | */ |
||
330 | 1 | public function getUrlRewritesByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
|
334 | |||
335 | /** |
||
336 | * Return's the URL rewrite product category relation for the passed |
||
337 | * URL rewrite ID. |
||
338 | * |
||
339 | * @param integer $urlRewriteId The URL rewrite ID to load the URL rewrite product category relation for |
||
340 | * |
||
341 | * @return array|false The URL rewrite product category relations |
||
342 | */ |
||
343 | 1 | protected function loadUrlRewriteProductCategory($urlRewriteId) |
|
347 | |||
348 | /** |
||
349 | * Delete's the URL rewrite with the passed attributes. |
||
350 | * |
||
351 | * @param array $row The attributes of the entity to delete |
||
352 | * @param string|null $name The name of the prepared statement that has to be executed |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | protected function deleteUrlRewrite($row, $name = null) |
||
360 | } |
||
361 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.