Skip to content

Commit

Permalink
Implement check of store related option values to avoid saving of err…
Browse files Browse the repository at this point in the history
…oneous mappings
  • Loading branch information
Mardl committed Aug 31, 2023
1 parent fa62323 commit 37b755d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Version 22.0.4

## Feature

* Implement check of store related option values to avoid saving of erroneous mappings

# Version 22.0.3

## Bugfixes
Expand Down
60 changes: 57 additions & 3 deletions src/Observers/AttributeOptionValueExportObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace TechDivision\Import\Attribute\Observers;

use TechDivision\Import\Attribute\Utils\ColumnKeys;
use TechDivision\Import\Utils\RegistryKeys;

/**
* Observer that exports the attribute options to an additional CSV file for further processing.
Expand Down Expand Up @@ -59,11 +60,20 @@ protected function process()
return $this->explode($value, $this->getMultipleFieldDelimiter());
});

$adminValueArtefacts = $this->getArtefactsByTypeAndEntityId(AttributeOptionExportObserver::ARTEFACT_TYPE, $this->getLastEntityId());

// validate the admin values with the option values
if (!$this->isValidateAdminValuesWithStoreOptionValues($adminValueArtefacts, $attributeOptionValues)) {
// Skip the export if the store values are not valid
return;
}

// iterate over the attribute option values and export them
foreach ($attributeOptionValues as $key => $attributeOptionValue) {
// load the artefacts with the admin store values
$adminValueArtefacts = $this->getArtefactsByTypeAndEntityId(AttributeOptionExportObserver::ARTEFACT_TYPE, $this->getLastEntityId());

// query whether or not the attribute option value is available
if (!isset($adminValueArtefacts[$key]) || empty($attributeOptionValue) || empty($adminValueArtefacts[$key])) {
continue;
}
// initialize and add the new artefact
$artefacts[] = $this->newArtefact(
array(
Expand Down Expand Up @@ -94,4 +104,48 @@ protected function getArtefactType()
{
return AttributeOptionValueExportObserver::ARTEFACT_TYPE;
}

/**
* @param array $adminValueArtefacts attribute option values from admin row
* @param array $attributeOptionValues attribute option values for the custom store views
* @return bool
* @throws \Exception
*/
protected function isValidateAdminValuesWithStoreOptionValues(array $adminValueArtefacts, $attributeOptionValues): bool
{
if (count($adminValueArtefacts) != count($attributeOptionValues)) {
$origin = array();
foreach ($adminValueArtefacts as $originalData) {
$origin[] = $originalData[ColumnKeys::VALUE];
}
$message =
sprintf(
"Store '%s' related number of options of attribute '%s' must be identical to the global definition (%d vs. %d). Global: '%s' vs. Store: '%s'",
$this->getStoreViewCode(),
$this->getValue(ColumnKeys::ATTRIBUTE_CODE),
count($adminValueArtefacts),
count($attributeOptionValues),
implode(',', $origin),
$this->getValue(ColumnKeys::ATTRIBUTE_OPTION_VALUES),
);
if (!$this->getSubject()->isStrictMode()) {
$this->getSystemLogger()->warning($message);
$this->getSubject()->mergeStatus(
array(
RegistryKeys::NO_STRICT_VALIDATIONS => array(
basename($this->getSubject()->getFilename()) => array(
$this->getSubject()->getLineNumber() => array(
ColumnKeys::ATTRIBUTE_OPTION_VALUES => $message
)
)
)
)
);
return false;
} else {
throw new \InvalidArgumentException($message);
}
}
return true;
}
}

0 comments on commit 37b755d

Please sign in to comment.