Skip to content

Commit da1833a

Browse files
author
Mohammad HAJ SALEM
committed
👌 apply code enhancement
1 parent 0b14b4e commit da1833a

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

app/code/Magento/Catalog/Model/Product/Link/SaveHandler.php

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ public function execute($entityType, $entity)
7777
$linksByType[$link->getLinkType()][] = $link;
7878
}
7979

80-
// Do check
81-
$hasPositionLinkType = $this->isPositionSet($linksByType);
82-
8380
// Set array position as a fallback position if necessary
84-
foreach ($hasPositionLinkType as $linkType => $hasPosition) {
85-
if (!$hasPosition) {
81+
foreach ($linksByType as $linkType => $links) {
82+
if (!$this->hasPosition($links)) {
8683
array_walk($linksByType[$linkType], function ($productLink, $position) {
8784
$productLink->setPosition(++$position);
8885
});
@@ -101,34 +98,17 @@ public function execute($entityType, $entity)
10198
}
10299

103100
/**
104-
* Check if the position is set for all product links per link type.
105-
* array with boolean per type
106-
*
107-
* @param $linksByType
108-
* @return array
101+
* Check if at least one link without position
102+
* @param $links
103+
* @return bool
109104
*/
110-
private function isPositionSet($linksByType)
105+
public function hasPosition($links)
111106
{
112-
$linkTypes = $this->linkTypeProvider->getLinkTypes();
113-
114-
// Initialize isPositionSet for existent link types
115-
$isPositionSet = [];
116-
foreach (array_keys($linkTypes) as $typeName) {
117-
if (array_key_exists($typeName, $linksByType)) {
118-
$isPositionSet[$typeName] = count($linksByType[$typeName]) > 0;
119-
}
120-
}
121-
122-
// Check if at least one link without position exists per Link type
123-
foreach ($linksByType as $type => $links) {
124-
foreach ($links as $link) {
125-
if (!array_key_exists('position', $link->getData())) {
126-
$isPositionSet[$type] = false;
127-
break;
128-
}
107+
foreach ($links as $link) {
108+
if (!array_key_exists('position', $link->getData())) {
109+
return false;
129110
}
130111
}
131-
132-
return $isPositionSet;
112+
return true;
133113
}
134114
}

0 commit comments

Comments
 (0)