Skip to content

Commit 6ee41ff

Browse files
[Magento Community Engineering] Community Contributions - 2.3-develop-prs
- merged with '2.3-develop-expedited-prs' branch
2 parents 8503ea9 + 253917a commit 6ee41ff

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

app/code/Magento/UrlRewrite/Model/Storage/DbStorage.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class DbStorage extends AbstractStorage
4949
private $logger;
5050

5151
/**
52-
* @param UrlRewriteFactory $urlRewriteFactory
53-
* @param DataObjectHelper $dataObjectHelper
54-
* @param ResourceConnection $resource
52+
* @param UrlRewriteFactory $urlRewriteFactory
53+
* @param DataObjectHelper $dataObjectHelper
54+
* @param ResourceConnection $resource
5555
* @param LoggerInterface|null $logger
5656
*/
5757
public function __construct(
@@ -71,7 +71,7 @@ public function __construct(
7171
/**
7272
* Prepare select statement for specific filter
7373
*
74-
* @param array $data
74+
* @param array $data
7575
* @return Select
7676
*/
7777
protected function prepareSelect(array $data)
@@ -106,12 +106,14 @@ protected function doFindOneByData(array $data)
106106

107107
$requestPath = $data[UrlRewrite::REQUEST_PATH];
108108
$decodedRequestPath = urldecode($requestPath);
109-
$data[UrlRewrite::REQUEST_PATH] = array_unique([
109+
$data[UrlRewrite::REQUEST_PATH] = array_unique(
110+
[
110111
rtrim($requestPath, '/'),
111112
rtrim($requestPath, '/') . '/',
112113
rtrim($decodedRequestPath, '/'),
113114
rtrim($decodedRequestPath, '/') . '/',
114-
]);
115+
]
116+
);
115117

116118
$resultsFromDb = $this->connection->fetchAll($this->prepareSelect($data));
117119
if ($resultsFromDb) {
@@ -128,8 +130,8 @@ protected function doFindOneByData(array $data)
128130
/**
129131
* Extract most relevant url rewrite from url rewrites list
130132
*
131-
* @param string $requestPath
132-
* @param array $urlRewrites
133+
* @param string $requestPath
134+
* @param array $urlRewrites
133135
* @return array|null
134136
*/
135137
private function extractMostRelevantUrlRewrite(string $requestPath, array $urlRewrites): ?array
@@ -166,8 +168,8 @@ private function extractMostRelevantUrlRewrite(string $requestPath, array $urlRe
166168
* If request path matches the DB value or it's redirect - we can return result from DB
167169
* Otherwise return 301 redirect to request path from DB results
168170
*
169-
* @param string $requestPath
170-
* @param array $urlRewrite
171+
* @param string $requestPath
172+
* @param array $urlRewrite
171173
* @return array
172174
*/
173175
private function prepareUrlRewrite(string $requestPath, array $urlRewrite): array
@@ -197,7 +199,7 @@ private function prepareUrlRewrite(string $requestPath, array $urlRewrite): arra
197199
/**
198200
* Delete old URLs from DB.
199201
*
200-
* @param UrlRewrite[] $urls
202+
* @param UrlRewrite[] $urls
201203
* @return void
202204
*/
203205
private function deleteOldUrls(array $urls): void
@@ -242,7 +244,7 @@ private function deleteOldUrls(array $urls): void
242244
/**
243245
* Prepare array with unique entities
244246
*
245-
* @param UrlRewrite[] $urls
247+
* @param UrlRewrite[] $urls
246248
* @return array
247249
*/
248250
private function prepareUniqueEntities(array $urls): array
@@ -258,23 +260,33 @@ private function prepareUniqueEntities(array $urls): array
258260
}
259261
$uniqueEntities[$url->getStoreId()][$url->getEntityType()] = $entityIds;
260262
}
263+
261264
return $uniqueEntities;
262265
}
263266

264267
/**
265268
* @inheritDoc
266269
*/
267-
protected function doReplace(array $urls)
270+
protected function doReplace(array $urls): array
268271
{
269-
$this->deleteOldUrls($urls);
272+
$this->connection->beginTransaction();
270273

271-
$data = [];
272-
foreach ($urls as $url) {
273-
$data[] = $url->toArray();
274-
}
275274
try {
275+
$this->deleteOldUrls($urls);
276+
277+
$data = [];
278+
foreach ($urls as $url) {
279+
$data[] = $url->toArray();
280+
}
281+
276282
$this->insertMultiple($data);
283+
284+
$this->connection->commit();
285+
// @codingStandardsIgnoreStart
277286
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
287+
// @codingStandardsIgnoreEnd
288+
$this->connection->rollBack();
289+
278290
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
279291
$urlConflicted = [];
280292
foreach ($urls as $url) {
@@ -298,6 +310,9 @@ protected function doReplace(array $urls)
298310
} else {
299311
throw $e->getPrevious() ?: $e;
300312
}
313+
} catch (\Exception $e) {
314+
$this->connection->rollBack();
315+
throw $e;
301316
}
302317

303318
return $urls;
@@ -306,12 +321,12 @@ protected function doReplace(array $urls)
306321
/**
307322
* Insert multiple
308323
*
309-
* @param array $data
324+
* @param array $data
310325
* @return void
311326
* @throws \Magento\Framework\Exception\AlreadyExistsException|\Exception
312327
* @throws \Exception
313328
*/
314-
protected function insertMultiple($data)
329+
protected function insertMultiple($data): void
315330
{
316331
try {
317332
$this->connection->insertMultiple($this->resource->getTableName(self::TABLE_NAME), $data);
@@ -331,11 +346,11 @@ protected function insertMultiple($data)
331346
/**
332347
* Get filter for url rows deletion due to provided urls
333348
*
334-
* @param UrlRewrite[] $urls
335-
* @return array
349+
* @param UrlRewrite[] $urls
350+
* @return array
336351
* @deprecated Not used anymore.
337352
*/
338-
protected function createFilterDataBasedOnUrls($urls)
353+
protected function createFilterDataBasedOnUrls($urls): array
339354
{
340355
$data = [];
341356
foreach ($urls as $url) {

0 commit comments

Comments
 (0)