Skip to content

METSUP:68: Fix url-conflict in import #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 25.0.5

## Bugfixes

* Handle Duplicate UrlKey Exception With StrictMode(METSUP-68):
* to make the error message for URL conflicts in the import contain more and more precise data than is currently the case

# 25.0.4

## Features
Expand Down
50 changes: 48 additions & 2 deletions src/Observers/UrlRewriteObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ protected function process()
try {
// persist the URL rewrite
if ($this->hasChanges($urlRewrite)) {
$this->urlRewriteId = $this->persistUrlRewrite($urlRewrite);
try {
$this->urlRewriteId = $this->persistUrlRewrite($urlRewrite);
} catch (\PDOException $pdoe) {
$this->handleDuplicateUrlKeyExceptionWithStrictMode($urlRewrite, $pdoe);
}
} else {
$this->urlRewriteId = $urlRewrite[MemberNames::URL_REWRITE_ID];
}
Expand All @@ -335,7 +339,11 @@ protected function process()

// persist the URL rewrite product category relation
if ($this->hasChanges($urlRewriteProductCategory)) {
$this->persistUrlRewriteProductCategory($urlRewriteProductCategory);
try {
$this->persistUrlRewriteProductCategory($urlRewriteProductCategory);
} catch (\PDOException $pdoe) {
$this->handleDuplicateUrlKeyExceptionWithStrictMode($urlRewriteProductCategory, $pdoe);
}
}
} catch (\Exception $e) {
if (!$this->getSubject()->isStrictMode()) {
Expand Down Expand Up @@ -859,4 +867,42 @@ protected function loadProduct($sku)
{
return $this->getProductUrlRewriteProcessor()->loadProduct($sku);
}

/**
* @param array $urlRewriteData data of urlrewrite
* @param \PDOException $pdoe pdo exception
* @return void
* @throws \PDOException
*/
public function handleDuplicateUrlKeyExceptionWithStrictMode(array $urlRewriteData, \PDOException $pdoe): void
{
$message = sprintf(
'Is a "Duplicate entry" PDO exception is thrown: with Urlrewrite Data \\n
("entity_id": "%s" && "request_path": "%s" && "target_path": "%s" && "entity_type": "%s" && "redirect_type": "%s" && "store_id": "%s")',
$urlRewriteData[MemberNames::ENTITY_ID],
$urlRewriteData[MemberNames::REQUEST_PATH],
$urlRewriteData[MemberNames::TARGET_PATH],
$urlRewriteData[MemberNames::ENTITY_TYPE],
$urlRewriteData[MemberNames::REDIRECT_TYPE],
$urlRewriteData[MemberNames::STORE_ID]
);
if (!$this->getSubject()->isStrictMode()) {
$this->getSubject()
->getSystemLogger()
->warning($this->getSubject()->appendExceptionSuffix($message));
$this->mergeStatus(
array(
RegistryKeys::NO_STRICT_VALIDATIONS => array(
basename($this->getFilename()) => array(
$this->getLineNumber() => array(
ColumnKeys::URL_KEY => $message
)
)
)
)
);
} else {
throw new \PDOException($pdoe);
}
}
}
36 changes: 34 additions & 2 deletions src/Observers/UrlRewriteUpdateObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace TechDivision\Import\Product\UrlRewrite\Observers;

use TechDivision\Import\Utils\RegistryKeys;
use TechDivision\Import\Utils\StoreViewCodes;
use TechDivision\Import\Product\Utils\CoreConfigDataKeys;
use TechDivision\Import\Product\UrlRewrite\Utils\MemberNames;
Expand Down Expand Up @@ -129,8 +130,39 @@ protected function process()
// merge and return the prepared URL rewrite
$existingUrlRewrite = $this->mergeEntity($existingUrlRewrite, $attr);

// create the URL rewrite
$this->persistUrlRewrite($existingUrlRewrite);
try {
// create the URL rewrite
$this->persistUrlRewrite($existingUrlRewrite);
} catch (\PDOException $pdoe) {
if (!$this->getSubject()->isStrictMode()) {
$message = sprintf(
'Is a "Duplicate entry" PDO exception is thrown: with Urlrewrite Data \\n
("entity_id": "%s" && "request_path": "%s" && "target_path": "%s" && "entity_type": "%s" && "redirect_type": "%s" && "store_id": "%s")',
$existingUrlRewrite[MemberNames::ENTITY_ID],
$existingUrlRewrite[MemberNames::REQUEST_PATH],
$existingUrlRewrite[MemberNames::TARGET_PATH],
$existingUrlRewrite[MemberNames::ENTITY_TYPE],
$existingUrlRewrite[MemberNames::REDIRECT_TYPE],
$existingUrlRewrite[MemberNames::STORE_ID]
);
$this->getSubject()
->getSystemLogger()
->warning($this->getSubject()->appendExceptionSuffix($message));
$this->mergeStatus(
array(
RegistryKeys::NO_STRICT_VALIDATIONS => array(
basename($this->getFilename()) => array(
$this->getLineNumber() => array(
ColumnKeys::URL_KEY => $message
)
)
)
)
);
} else {
throw new \PDOException($pdoe);
}
}
} else {
// query whether or not the URL rewrite has to be removed
if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_URL_REWRITES) &&
Expand Down