Skip to content

Commit

Permalink
magento#18624 Refactoring: Flattening Conditional tree using early "c…
Browse files Browse the repository at this point in the history
…ontinue"
  • Loading branch information
amenk committed Mar 8, 2019
1 parent 906abf2 commit 67bab68
Showing 1 changed file with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Magento\Catalog\Model\ResourceModel\Product\LinkFactory;
use Magento\CatalogImportExport\Model\Import\Product;
use Magento\CatalogImportExport\Model\Import\Product\SkuProcessor;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\ImportExport\Model\Import;
use Magento\ImportExport\Model\ResourceModel\Helper;
Expand Down Expand Up @@ -97,64 +96,68 @@ public function process()
}
foreach ($this->_linkNameToId as $linkName => $linkId) {
$productIds[] = $productId;
if (isset($rowData[$linkName . 'sku'])) {
$linkSkus = explode($this->_entityModel->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
$linkPositions = !empty($rowData[$linkName . 'position'])
? explode($this->_entityModel->getMultipleValueSeparator(), $rowData[$linkName . 'position'])
: [];
foreach ($linkSkus as $linkedKey => $linkedSku) {
$linkedSku = trim($linkedSku);
if (($this->skuProcessor->getNewSku($linkedSku) !== null || $this->isSkuExist($linkedSku))
&& strcasecmp($linkedSku, $sku) !== 0
) {
$newSku = $this->skuProcessor->getNewSku($linkedSku);
if (!empty($newSku)) {
$linkedId = $newSku['entity_id'];
} else {
$linkedId = $this->getExistingSku($linkedSku)['entity_id'];
}
if (!isset($rowData[$linkName . 'sku'])) {
continue;
}

$linkSkus = explode($this->_entityModel->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
$linkPositions = !empty($rowData[$linkName . 'position'])
? explode($this->_entityModel->getMultipleValueSeparator(), $rowData[$linkName . 'position'])
: [];
foreach ($linkSkus as $linkedKey => $linkedSku) {
$linkedSku = trim($linkedSku);
if (!(($this->skuProcessor->getNewSku($linkedSku) !== null || $this->isSkuExist($linkedSku))
&& strcasecmp($linkedSku, $sku) !== 0)
) {
continue;
}

$newSku = $this->skuProcessor->getNewSku($linkedSku);
if (!empty($newSku)) {
$linkedId = $newSku['entity_id'];
} else {
$linkedId = $this->getExistingSku($linkedSku)['entity_id'];
}

if ($linkedId == null) {
// Import file links to a SKU which is skipped for some reason,
// which leads to a "NULL"
// link causing fatal errors.
$this->_logger->critical(
new \Exception(
sprintf(
'WARNING: Orphaned link skipped: From SKU %s (ID %d) to SKU %s, ' .
'Link type id: %d',
$sku,
$productId,
$linkedSku,
$linkId
)
)
);
continue;
}
if ($linkedId == null) {
// Import file links to a SKU which is skipped for some reason,
// which leads to a "NULL"
// link causing fatal errors.
$this->_logger->critical(
new \Exception(
sprintf(
'WARNING: Orphaned link skipped: From SKU %s (ID %d) to SKU %s, ' .
'Link type id: %d',
$sku,
$productId,
$linkedSku,
$linkId
)
)
);
continue;
}

$linkKey = "{$productId}-{$linkedId}-{$linkId}";
if (empty($productLinkKeys[$linkKey])) {
$productLinkKeys[$linkKey] = $nextLinkId;
}
if (!isset($linkRows[$linkKey])) {
$linkRows[$linkKey] = [
'link_id' => $productLinkKeys[$linkKey],
'product_id' => $productId,
'linked_product_id' => $linkedId,
'link_type_id' => $linkId,
];
}
if (!empty($linkPositions[$linkedKey])) {
$positionRows[] = [
'link_id' => $productLinkKeys[$linkKey],
'product_link_attribute_id' => $positionAttrId[$linkId],
'value' => $linkPositions[$linkedKey],
];
}
$nextLinkId++;
}
$linkKey = "{$productId}-{$linkedId}-{$linkId}";
if (empty($productLinkKeys[$linkKey])) {
$productLinkKeys[$linkKey] = $nextLinkId;
}
if (!isset($linkRows[$linkKey])) {
$linkRows[$linkKey] = [
'link_id' => $productLinkKeys[$linkKey],
'product_id' => $productId,
'linked_product_id' => $linkedId,
'link_type_id' => $linkId,
];
}
if (!empty($linkPositions[$linkedKey])) {
$positionRows[] = [
'link_id' => $productLinkKeys[$linkKey],
'product_link_attribute_id' => $positionAttrId[$linkId],
'value' => $linkPositions[$linkedKey],
];
}
$nextLinkId++;
}
}
}
Expand Down

0 comments on commit 67bab68

Please sign in to comment.