Skip to content

Commit a3dd257

Browse files
authored
Merge pull request #91 from tuyennn/feature/verbose-mode-handling-duplicate-urls
[feat] Move logging detect Duplicated url on current product to verbo…
2 parents ced866b + 3e47025 commit a3dd257

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Arguments:
3333
Options:
3434
--store (-s) Use a specific store (store Id, store code or 'all')
3535
--root (-r) Regenerate for root category and its children, ignoring cids.
36+
--verbose (-v) Verbose mode to display the errors. Eg: duplicated product urls
3637
--help (-h) Display this help message
3738
```
3839

src/Service/RegenerateProductUrl.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class RegenerateProductUrl
5555
*/
5656
private int $regeneratedCount = 0;
5757

58+
/**
59+
* @var bool
60+
*/
61+
private bool $isVerboseMode = false;
62+
5863
/**
5964
* Constructor.
6065
*
@@ -87,11 +92,12 @@ public function __construct(
8792
*/
8893
public function execute(?array $productIds = null, ?int $storeId = null, bool $verbose = false): void
8994
{
95+
$this->isVerboseMode = $verbose;
9096
$this->regeneratedCount = 0;
9197

9298
$stores = null !== $storeId
9399
? [$this->storeManager->getStore($storeId)]
94-
: $this->storeManager->getStores(false);
100+
: $this->storeManager->getStores();
95101

96102
foreach ($stores as $store) {
97103
$regeneratedForStore = 0;
@@ -132,7 +138,7 @@ public function execute(?array $productIds = null, ?int $storeId = null, bool $v
132138
$newUrls = [];
133139
/** @var Product $product */
134140
foreach ($collection as $product) {
135-
if ($verbose) {
141+
if ($this->isVerboseMode) {
136142
$this->log(
137143
sprintf(
138144
'Regenerating urls for %s (%s) in store (%s)',
@@ -216,19 +222,27 @@ private function replaceUrls(array &$urls, bool $last = false): int
216222
{
217223
$this->log(sprintf('replaceUrls%s batch: %d', $last ? ' last' : '', count($urls)));
218224

219-
foreach ($urls as $url) {
225+
if ($this->isVerboseMode) {
226+
foreach ($urls as $url) {
227+
try {
228+
$this->urlPersist->replace([$url]);
229+
} catch (Exception $e) {
230+
$this->log(
231+
sprintf(
232+
$e->getMessage() . ' Entity id: %d Request path: %s',
233+
$url->getEntityId(),
234+
$url->getRequestPath()
235+
)
236+
);
237+
}
238+
}
239+
} else {
220240
try {
221-
$this->urlPersist->replace([$url]);
222-
} catch (Exception $e) {
223-
$this->log(
224-
sprintf(
225-
$e->getMessage() . ' Entity id: %d Request path: %s',
226-
$url->getEntityId(),
227-
$url->getRequestPath()
228-
)
229-
);
241+
$this->urlPersist->replace($urls);
242+
} catch (Exception $e) {//phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
230243
}
231244
}
245+
232246
$count = count($urls);
233247
$urls = [];
234248

0 commit comments

Comments
 (0)