Skip to content

Commit

Permalink
Merge pull request #88 from Gerke-W/feature/create-better-error-handling
Browse files Browse the repository at this point in the history
Feature/create better error handling
  • Loading branch information
peterjaap authored Jul 24, 2024
2 parents 859253f + b742038 commit 7ecf75a
Showing 1 changed file with 36 additions and 53 deletions.
89 changes: 36 additions & 53 deletions src/Service/RegenerateProductUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,56 +109,29 @@ public function execute(?array $productIds = null, ?int $storeId = null, bool $v
}

$newUrls = [];
try {
/** @var Product $product */
foreach ($collection as $product) {
if ($verbose) {
$this->log(
sprintf(
'Regenerating urls for %s (%s) in store (%s)',
$product->getSku(),
$product->getId(),
$store->getName()
)
);
}

$product->setStoreId($store->getId());

$newUrls = array_merge($newUrls, $this->urlRewriteGenerator->generate($product));
if (count($newUrls) >= self::BATCH_SIZE) {
$regeneratedForStore += $this->replaceUrls($newUrls);
}
/** @var Product $product */
foreach ($collection as $product) {
if ($verbose) {
$this->log(
sprintf(
'Regenerating urls for %s (%s) in store (%s)',
$product->getSku(),
$product->getId(),
$store->getName()
)
);
}

if (count($newUrls)) {
$regeneratedForStore += $this->replaceUrls($newUrls, true);
$product->setStoreId($store->getId());

$newUrls = array_merge($newUrls, $this->urlRewriteGenerator->generate($product));
if (count($newUrls) >= self::BATCH_SIZE) {
$regeneratedForStore += $this->replaceUrls($newUrls);
}
} catch (\Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException $e) {
$this->log(sprintf(
'<error>Couldn\'t insert duplicate URL rewrites for the following ' .
'products on store ID %d (current batch failed):' . PHP_EOL . '%s</error>',
$store->getId(),
implode(PHP_EOL, array_map(function ($url) {
return sprintf(
'- Product ID: %d, request path: %s',
$url['entity_id'],
$url['request_path']
);
}, $e->getUrls()))
));
} catch (Exception $e) {
$this->log(
sprintf(
'<error>Duplicated url for store ID %d, product %d (%s) - %s Generated URLs:' .
PHP_EOL . '%s</error>' . PHP_EOL,
$store->getId(),
$product->getId(),
$product->getSku(),
$e->getMessage(),
implode(PHP_EOL, array_keys($newUrls))
)
);
}

if (count($newUrls)) {
$regeneratedForStore += $this->replaceUrls($newUrls, true);
}

$this->log(
Expand Down Expand Up @@ -204,18 +177,28 @@ private function log(string $message): void
}

/**
* Add product ulrs
*
* @param array $urls
* @param bool $last
* @param bool $last
*
* @return int
* @throws UrlAlreadyExistsException
*/
private function replaceUrls(array &$urls, bool $last = false): int
{
$this->log(sprintf('replaceUrls%s batch: %d', $last ? ' last' : '', count($urls)));
$this->urlPersist->replace($urls);

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()
)
);
}
}
$count = count($urls);
$urls = [];

Expand All @@ -233,7 +216,7 @@ private function replaceUrls(array &$urls, bool $last = false): int
*/
private function deleteUrls(array &$productIds, StoreInterface $store, bool $last = false): void
{
$this->log(sprintf('deleteUrls%s batch: %d', $last ? 'last' : '', count($productIds)));
$this->log(sprintf('deleteUrls%s batch: %d', $last ? ' last' : '', count($productIds)));
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $productIds,
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
Expand Down

0 comments on commit 7ecf75a

Please sign in to comment.