Skip to content

#18624 Refactoring: Extract addLinks to own class #21658

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
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8b73246
#18624 Refactoring: Extract addLinks to own class without BC break
amenk Mar 8, 2019
906abf2
#18624 B/C Break: Move linkNameToId to LinkProcessor
amenk Mar 8, 2019
67bab68
#18624 Refactoring: Flattening Conditional tree using early "continue"
amenk Mar 8, 2019
3a70e41
#18624 Refactoring: Decompose process Funktion; add Changelog with BC…
amenk Mar 8, 2019
49b62cf
#18624 Refactoring: Create LinkProcessor via DI
amenk Mar 9, 2019
f8fcb2e
#18624 Refactoring: Extract linkNameToId to di config
amenk Mar 9, 2019
5be2075
#18624 Refactoring: Type hinting and remove unused parameter
amenk Mar 9, 2019
28cd9d1
#18624 Refactoring: Extract Database code
amenk Mar 9, 2019
10e68bc
#18624 Refactoring: Extract code
amenk Mar 9, 2019
87f4c35
#18624 Refactoring: Bugfix: getExistingSku needs to be public
amenk Mar 9, 2019
0aceed9
#18624 Fix: Unknown Method (Make Public in Entity Model, and use that)
amenk Mar 13, 2019
ea2bed1
Make protected methods and variables private, remove "_"
amenk Mar 24, 2019
c34bfa1
Remove changelog
amenk Mar 24, 2019
aa559f8
Re-add $_linkNameToId in Product parent class, add (deprecated) BC layer
amenk Mar 24, 2019
08701d9
Fix Method Naming (for BC reasons)
amenk Mar 24, 2019
019937b
Move comment to method
amenk Mar 24, 2019
f250282
Merge remote-tracking branch 'origin/patch-refactor-importexport-link…
amenk Mar 24, 2019
3e6499d
WIP: Move Database code to ResourceModel
amenk Apr 1, 2019
d1962de
Finalize: Move Database code to ResourceModel
amenk Apr 6, 2019
cabf925
Fix codacy warnings that make sense
amenk Apr 6, 2019
dc251d4
Codacy: Fix Variable Name and unneccesary assignment
amenk Apr 6, 2019
ad7d915
Fix/Refactor: Do not inject empty array to function
amenk Apr 6, 2019
088dcc9
Linebreaks...
amenk Apr 6, 2019
bbcd23e
Merge branch '2.3-develop' into patch-refactor-importexport-links
amenk May 8, 2019
af0e222
Merge remote-tracking branch 'upstream/2.3-develop' into patch-refact…
amenk May 19, 2019
d589e67
Fix Bug: Links are not passed loadPositionAttributes
amenk May 19, 2019
927e4d8
Use Const in di to specific linkNameToId
amenk May 19, 2019
75a28a7
Fix Code Style + Copyright info
amenk May 19, 2019
5f7a3d5
Decompose saveLinks
amenk May 19, 2019
0543073
Fix failing failing testProductsWithMultipleStoresWhenMediaIsDisabled
amenk May 19, 2019
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
149 changes: 19 additions & 130 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\Config as CatalogConfig;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogImportExport\Model\Import\Product\ImageTypeProcessor;
use Magento\CatalogImportExport\Model\Import\Product\LinkProcessor;
use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
Expand Down Expand Up @@ -219,7 +220,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity

/**
* Links attribute name-to-link type ID.
*
* @deprecated kept for BC, use DI to inject to LinkProcessor
* @var array
*/
protected $_linkNameToId = [
Expand Down Expand Up @@ -731,6 +732,12 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $mediaProcessor;

/**
* @var LinkProcessor
*/
private $linkProcessor;


/**
* @var DateTimeFactory
*/
Expand Down Expand Up @@ -835,7 +842,8 @@ public function __construct(
MediaGalleryProcessor $mediaProcessor = null,
StockItemImporterInterface $stockItemImporter = null,
DateTimeFactory $dateTimeFactory = null,
ProductRepositoryInterface $productRepository = null
ProductRepositoryInterface $productRepository = null,
LinkProcessor $linkProcessor = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -891,6 +899,9 @@ public function __construct(
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
->get(ProductRepositoryInterface::class);
$this->linkProcessor = $linkProcessor ?? ObjectManager::getInstance()
->get(LinkProcessor::class);
$this->linkProcessor->addNameToIds($this->_linkNameToId);
}

/**
Expand Down Expand Up @@ -1111,7 +1122,7 @@ protected function _saveProductsData()
foreach ($this->_productTypeModels as $productTypeModel) {
$productTypeModel->saveData();
}
$this->_saveLinks();
$this->linkProcessor->saveLinks($this, $this->getProductEntityLinkField());
$this->_saveStockItem();
if ($this->_replaceFlag) {
$this->getOptionEntity()->clearProductsSkuToId();
Expand Down Expand Up @@ -1244,134 +1255,12 @@ protected function _prepareRowForDb(array $rowData)
* Must be called after ALL products saving done.
*
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
* @deprecated use linkProcessor
*/
protected function _saveLinks()
{
$resource = $this->_linkFactory->create();
$mainTable = $resource->getMainTable();
$positionAttrId = [];
$nextLinkId = $this->_resourceHelper->getNextAutoincrement($mainTable);

// pre-load 'position' attributes ID for each link type once
foreach ($this->_linkNameToId as $linkName => $linkId) {
$select = $this->_connection->select()->from(
$resource->getTable('catalog_product_link_attribute'),
['id' => 'product_link_attribute_id']
)->where(
'link_type_id = :link_id AND product_link_attribute_code = :position'
);
$bind = [':link_id' => $linkId, ':position' => 'position'];
$positionAttrId[$linkId] = $this->_connection->fetchOne($select, $bind);
}
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$productIds = [];
$linkRows = [];
$positionRows = [];

foreach ($bunch as $rowNum => $rowData) {
if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
continue;
}

$sku = $rowData[self::COL_SKU];

$productId = $this->skuProcessor->getNewSku($sku)[$this->getProductEntityLinkField()];
$productLinkKeys = [];
$select = $this->_connection->select()->from(
$resource->getTable('catalog_product_link'),
['id' => 'link_id', 'linked_id' => 'linked_product_id', 'link_type_id' => 'link_type_id']
)->where(
'product_id = :product_id'
);
$bind = [':product_id' => $productId];
foreach ($this->_connection->fetchAll($select, $bind) as $linkData) {
$linkKey = "{$productId}-{$linkData['linked_id']}-{$linkData['link_type_id']}";
$productLinkKeys[$linkKey] = $linkData['id'];
}
foreach ($this->_linkNameToId as $linkName => $linkId) {
$productIds[] = $productId;
if (isset($rowData[$linkName . 'sku'])) {
$linkSkus = explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
$linkPositions = !empty($rowData[$linkName . 'position'])
? explode($this->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 ($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++;
}
}
}
}
}
if (Import::BEHAVIOR_APPEND != $this->getBehavior() && $productIds) {
$this->_connection->delete(
$mainTable,
$this->_connection->quoteInto('product_id IN (?)', array_unique($productIds))
);
}
if ($linkRows) {
$this->_connection->insertOnDuplicate($mainTable, $linkRows, ['link_id']);
}
if ($positionRows) {
// process linked product positions
$this->_connection->insertOnDuplicate(
$resource->getAttributeTypeTable('int'),
$positionRows,
['value']
);
}
}
$this->linkProcessor->process($this, $this->getProductEntityLinkField());
return $this;
}

Expand Down Expand Up @@ -3018,7 +2907,7 @@ private function parseMultipleValues($labelRow)
* @param string $sku
* @return bool
*/
private function isSkuExist($sku)
public function isSkuExist($sku)
{
$sku = strtolower($sku);
return isset($this->_oldSku[$sku]);
Expand All @@ -3030,7 +2919,7 @@ private function isSkuExist($sku)
* @param string $sku
* @return array
*/
private function getExistingSku($sku)
public function getExistingSku($sku)
{
return $this->_oldSku[strtolower($sku)];
}
Expand Down
Loading