@@ -49,9 +49,9 @@ class DbStorage extends AbstractStorage
49
49
private $ logger ;
50
50
51
51
/**
52
- * @param UrlRewriteFactory $urlRewriteFactory
53
- * @param DataObjectHelper $dataObjectHelper
54
- * @param ResourceConnection $resource
52
+ * @param UrlRewriteFactory $urlRewriteFactory
53
+ * @param DataObjectHelper $dataObjectHelper
54
+ * @param ResourceConnection $resource
55
55
* @param LoggerInterface|null $logger
56
56
*/
57
57
public function __construct (
@@ -71,7 +71,7 @@ public function __construct(
71
71
/**
72
72
* Prepare select statement for specific filter
73
73
*
74
- * @param array $data
74
+ * @param array $data
75
75
* @return Select
76
76
*/
77
77
protected function prepareSelect (array $ data )
@@ -106,12 +106,14 @@ protected function doFindOneByData(array $data)
106
106
107
107
$ requestPath = $ data [UrlRewrite::REQUEST_PATH ];
108
108
$ decodedRequestPath = urldecode ($ requestPath );
109
- $ data [UrlRewrite::REQUEST_PATH ] = array_unique ([
109
+ $ data [UrlRewrite::REQUEST_PATH ] = array_unique (
110
+ [
110
111
rtrim ($ requestPath , '/ ' ),
111
112
rtrim ($ requestPath , '/ ' ) . '/ ' ,
112
113
rtrim ($ decodedRequestPath , '/ ' ),
113
114
rtrim ($ decodedRequestPath , '/ ' ) . '/ ' ,
114
- ]);
115
+ ]
116
+ );
115
117
116
118
$ resultsFromDb = $ this ->connection ->fetchAll ($ this ->prepareSelect ($ data ));
117
119
if ($ resultsFromDb ) {
@@ -128,8 +130,8 @@ protected function doFindOneByData(array $data)
128
130
/**
129
131
* Extract most relevant url rewrite from url rewrites list
130
132
*
131
- * @param string $requestPath
132
- * @param array $urlRewrites
133
+ * @param string $requestPath
134
+ * @param array $urlRewrites
133
135
* @return array|null
134
136
*/
135
137
private function extractMostRelevantUrlRewrite (string $ requestPath , array $ urlRewrites ): ?array
@@ -166,8 +168,8 @@ private function extractMostRelevantUrlRewrite(string $requestPath, array $urlRe
166
168
* If request path matches the DB value or it's redirect - we can return result from DB
167
169
* Otherwise return 301 redirect to request path from DB results
168
170
*
169
- * @param string $requestPath
170
- * @param array $urlRewrite
171
+ * @param string $requestPath
172
+ * @param array $urlRewrite
171
173
* @return array
172
174
*/
173
175
private function prepareUrlRewrite (string $ requestPath , array $ urlRewrite ): array
@@ -197,7 +199,7 @@ private function prepareUrlRewrite(string $requestPath, array $urlRewrite): arra
197
199
/**
198
200
* Delete old URLs from DB.
199
201
*
200
- * @param UrlRewrite[] $urls
202
+ * @param UrlRewrite[] $urls
201
203
* @return void
202
204
*/
203
205
private function deleteOldUrls (array $ urls ): void
@@ -242,7 +244,7 @@ private function deleteOldUrls(array $urls): void
242
244
/**
243
245
* Prepare array with unique entities
244
246
*
245
- * @param UrlRewrite[] $urls
247
+ * @param UrlRewrite[] $urls
246
248
* @return array
247
249
*/
248
250
private function prepareUniqueEntities (array $ urls ): array
@@ -258,23 +260,33 @@ private function prepareUniqueEntities(array $urls): array
258
260
}
259
261
$ uniqueEntities [$ url ->getStoreId ()][$ url ->getEntityType ()] = $ entityIds ;
260
262
}
263
+
261
264
return $ uniqueEntities ;
262
265
}
263
266
264
267
/**
265
268
* @inheritDoc
266
269
*/
267
- protected function doReplace (array $ urls )
270
+ protected function doReplace (array $ urls ): array
268
271
{
269
- $ this ->deleteOldUrls ( $ urls );
272
+ $ this ->connection -> beginTransaction ( );
270
273
271
- $ data = [];
272
- foreach ($ urls as $ url ) {
273
- $ data [] = $ url ->toArray ();
274
- }
275
274
try {
275
+ $ this ->deleteOldUrls ($ urls );
276
+
277
+ $ data = [];
278
+ foreach ($ urls as $ url ) {
279
+ $ data [] = $ url ->toArray ();
280
+ }
281
+
276
282
$ this ->insertMultiple ($ data );
283
+
284
+ $ this ->connection ->commit ();
285
+ // @codingStandardsIgnoreStart
277
286
} catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
287
+ // @codingStandardsIgnoreEnd
288
+ $ this ->connection ->rollBack ();
289
+
278
290
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
279
291
$ urlConflicted = [];
280
292
foreach ($ urls as $ url ) {
@@ -298,6 +310,9 @@ protected function doReplace(array $urls)
298
310
} else {
299
311
throw $ e ->getPrevious () ?: $ e ;
300
312
}
313
+ } catch (\Exception $ e ) {
314
+ $ this ->connection ->rollBack ();
315
+ throw $ e ;
301
316
}
302
317
303
318
return $ urls ;
@@ -306,12 +321,12 @@ protected function doReplace(array $urls)
306
321
/**
307
322
* Insert multiple
308
323
*
309
- * @param array $data
324
+ * @param array $data
310
325
* @return void
311
326
* @throws \Magento\Framework\Exception\AlreadyExistsException|\Exception
312
327
* @throws \Exception
313
328
*/
314
- protected function insertMultiple ($ data )
329
+ protected function insertMultiple ($ data ): void
315
330
{
316
331
try {
317
332
$ this ->connection ->insertMultiple ($ this ->resource ->getTableName (self ::TABLE_NAME ), $ data );
@@ -331,11 +346,11 @@ protected function insertMultiple($data)
331
346
/**
332
347
* Get filter for url rows deletion due to provided urls
333
348
*
334
- * @param UrlRewrite[] $urls
335
- * @return array
349
+ * @param UrlRewrite[] $urls
350
+ * @return array
336
351
* @deprecated Not used anymore.
337
352
*/
338
- protected function createFilterDataBasedOnUrls ($ urls )
353
+ protected function createFilterDataBasedOnUrls ($ urls ): array
339
354
{
340
355
$ data = [];
341
356
foreach ($ urls as $ url ) {
0 commit comments