Skip to content

Commit 260dd64

Browse files
ENGCOM-6983: #26800 Fixed Undefined variable in ProductLink/Management #26979
2 parents 0053c20 + d26a2b7 commit 260dd64

File tree

3 files changed

+315
-165
lines changed

3 files changed

+315
-165
lines changed

app/code/Magento/Catalog/Api/ProductLinkManagementInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface ProductLinkManagementInterface
1717
*
1818
* @param string $sku
1919
* @param string $type
20+
* @throws \Magento\Framework\Exception\NoSuchEntityException
2021
* @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
2122
*/
2223
public function getLinkedItemsByType($sku, $type);
@@ -28,6 +29,7 @@ public function getLinkedItemsByType($sku, $type);
2829
* @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $items
2930
* @throws \Magento\Framework\Exception\NoSuchEntityException
3031
* @throws \Magento\Framework\Exception\CouldNotSaveException
32+
* @throws \Magento\Framework\Exception\InputException
3133
* @return bool
3234
*/
3335
public function setProductLinks($sku, array $items);

app/code/Magento/Catalog/Model/ProductLink/Management.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,42 @@
66

77
namespace Magento\Catalog\Model\ProductLink;
88

9-
use Magento\Catalog\Api\Data;
109
use Magento\Framework\Exception\CouldNotSaveException;
1110
use Magento\Framework\Exception\NoSuchEntityException;
1211
use Magento\Framework\Exception\InputException;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Catalog\Model\Product\LinkTypeProvider;
14+
use Magento\Catalog\Api\ProductLinkManagementInterface;
1315

14-
class Management implements \Magento\Catalog\Api\ProductLinkManagementInterface
16+
/**
17+
* Manage product links from api
18+
*/
19+
class Management implements ProductLinkManagementInterface
1520
{
1621
/**
17-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
22+
* @var ProductRepositoryInterface
1823
*/
1924
protected $productRepository;
2025

2126
/**
22-
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
27+
* @var LinkTypeProvider
2328
*/
2429
protected $linkTypeProvider;
2530

2631
/**
27-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
28-
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
32+
* @param ProductRepositoryInterface $productRepository
33+
* @param LinkTypeProvider $linkTypeProvider
2934
*/
3035
public function __construct(
31-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
32-
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
36+
ProductRepositoryInterface $productRepository,
37+
LinkTypeProvider $linkTypeProvider
3338
) {
3439
$this->productRepository = $productRepository;
3540
$this->linkTypeProvider = $linkTypeProvider;
3641
}
3742

3843
/**
39-
* {@inheritdoc}
44+
* @inheritdoc
4045
*/
4146
public function getLinkedItemsByType($sku, $type)
4247
{
@@ -63,47 +68,42 @@ public function getLinkedItemsByType($sku, $type)
6368
}
6469

6570
/**
66-
* {@inheritdoc}
71+
* @inheritdoc
6772
*/
6873
public function setProductLinks($sku, array $items)
6974
{
75+
76+
if (empty($items)) {
77+
throw InputException::invalidFieldValue('items', 'empty array');
78+
}
79+
7080
$linkTypes = $this->linkTypeProvider->getLinkTypes();
7181

7282
// Check if product link type is set and correct
73-
if (!empty($items)) {
74-
foreach ($items as $newLink) {
75-
$type = $newLink->getLinkType();
76-
if ($type == null) {
77-
throw InputException::requiredField("linkType");
78-
}
79-
if (!isset($linkTypes[$type])) {
80-
throw new NoSuchEntityException(
81-
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
82-
);
83-
}
83+
foreach ($items as $newLink) {
84+
$type = $newLink->getLinkType();
85+
if ($type == null) {
86+
throw InputException::requiredField("linkType");
87+
}
88+
if (!isset($linkTypes[$type])) {
89+
throw new NoSuchEntityException(
90+
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
91+
);
8492
}
8593
}
8694

8795
$product = $this->productRepository->get($sku);
8896

89-
// Replace only links of the specified type
9097
$existingLinks = $product->getProductLinks();
91-
$newLinks = [];
92-
if (!empty($existingLinks)) {
93-
foreach ($existingLinks as $link) {
94-
if ($link->getLinkType() != $type) {
95-
$newLinks[] = $link;
96-
}
97-
}
98-
$newLinks = array_merge($newLinks, $items);
99-
} else {
100-
$newLinks = $items;
101-
}
98+
$newLinks = array_merge($existingLinks, $items);
99+
102100
$product->setProductLinks($newLinks);
103101
try {
104102
$this->productRepository->save($product);
105103
} catch (\Exception $exception) {
106-
throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
104+
throw new CouldNotSaveException(
105+
__('The linked products data is invalid. Verify the data and try again.')
106+
);
107107
}
108108

109109
return true;

0 commit comments

Comments
 (0)