Skip to content

Commit

Permalink
Merge pull request #91 from tuyennn/feature/verbose-mode-handling-dup…
Browse files Browse the repository at this point in the history
…licate-urls

[feat] Move logging detect Duplicated url on current product to verbo…
  • Loading branch information
peterjaap authored Aug 8, 2024
2 parents ced866b + 3e47025 commit a3dd257
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Arguments:
Options:
--store (-s) Use a specific store (store Id, store code or 'all')
--root (-r) Regenerate for root category and its children, ignoring cids.
--verbose (-v) Verbose mode to display the errors. Eg: duplicated product urls
--help (-h) Display this help message
```

Expand Down
38 changes: 26 additions & 12 deletions src/Service/RegenerateProductUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class RegenerateProductUrl
*/
private int $regeneratedCount = 0;

/**
* @var bool
*/
private bool $isVerboseMode = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -87,11 +92,12 @@ public function __construct(
*/
public function execute(?array $productIds = null, ?int $storeId = null, bool $verbose = false): void
{
$this->isVerboseMode = $verbose;
$this->regeneratedCount = 0;

$stores = null !== $storeId
? [$this->storeManager->getStore($storeId)]
: $this->storeManager->getStores(false);
: $this->storeManager->getStores();

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

foreach ($urls as $url) {
if ($this->isVerboseMode) {
foreach ($urls as $url) {
try {
$this->urlPersist->replace([$url]);
} catch (Exception $e) {
$this->log(
sprintf(
$e->getMessage() . ' Entity id: %d Request path: %s',
$url->getEntityId(),
$url->getRequestPath()
)
);
}
}
} else {
try {
$this->urlPersist->replace([$url]);
} catch (Exception $e) {
$this->log(
sprintf(
$e->getMessage() . ' Entity id: %d Request path: %s',
$url->getEntityId(),
$url->getRequestPath()
)
);
$this->urlPersist->replace($urls);
} catch (Exception $e) {//phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
}
}

$count = count($urls);
$urls = [];

Expand Down

0 comments on commit a3dd257

Please sign in to comment.