Skip to content

Commit 84eb1fc

Browse files
committed
#26800 Fixed Undefined variable in ProductLink/Management
1 parent 23fdbe4 commit 84eb1fc

File tree

3 files changed

+320
-167
lines changed

3 files changed

+320
-167
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace Magento\Catalog\Api;
88

9+
use Magento\Catalog\Api\Data\ProductLinkInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Exception\CouldNotSaveException;
12+
use Magento\Framework\Exception\InputException;
13+
914
/**
1015
* @api
1116
* @since 100.0.2
@@ -17,17 +22,19 @@ interface ProductLinkManagementInterface
1722
*
1823
* @param string $sku
1924
* @param string $type
20-
* @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
25+
* @throws NoSuchEntityException
26+
* @return ProductLinkInterface[]
2127
*/
2228
public function getLinkedItemsByType($sku, $type);
2329

2430
/**
2531
* Assign a product link to another product
2632
*
2733
* @param string $sku
28-
* @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $items
29-
* @throws \Magento\Framework\Exception\NoSuchEntityException
30-
* @throws \Magento\Framework\Exception\CouldNotSaveException
34+
* @param ProductLinkInterface[] $items
35+
* @throws NoSuchEntityException
36+
* @throws CouldNotSaveException
37+
* @throws InputException
3138
* @return bool
3239
*/
3340
public function setProductLinks($sku, array $items);

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@
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+
class Management implements ProductLinkManagementInterface
1517
{
1618
/**
17-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
19+
* @var ProductRepositoryInterface
1820
*/
1921
protected $productRepository;
2022

2123
/**
22-
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
24+
* @var LinkTypeProvider
2325
*/
2426
protected $linkTypeProvider;
2527

2628
/**
27-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
28-
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
29+
* @param ProductRepositoryInterface $productRepository
30+
* @param LinkTypeProvider $linkTypeProvider
2931
*/
3032
public function __construct(
31-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
32-
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
33+
ProductRepositoryInterface $productRepository,
34+
LinkTypeProvider $linkTypeProvider
3335
) {
3436
$this->productRepository = $productRepository;
3537
$this->linkTypeProvider = $linkTypeProvider;
@@ -67,43 +69,38 @@ public function getLinkedItemsByType($sku, $type)
6769
*/
6870
public function setProductLinks($sku, array $items)
6971
{
72+
73+
if (empty($items)) {
74+
throw InputException::invalidFieldValue('items', 'empty array');
75+
}
76+
7077
$linkTypes = $this->linkTypeProvider->getLinkTypes();
7178

7279
// 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-
}
80+
foreach ($items as $newLink) {
81+
$type = $newLink->getLinkType();
82+
if ($type == null) {
83+
throw InputException::requiredField("linkType");
84+
}
85+
if (!isset($linkTypes[$type])) {
86+
throw new NoSuchEntityException(
87+
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
88+
);
8489
}
8590
}
8691

8792
$product = $this->productRepository->get($sku);
8893

89-
// Replace only links of the specified type
9094
$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-
}
95+
$newLinks = array_merge($existingLinks, $items);
96+
10297
$product->setProductLinks($newLinks);
10398
try {
10499
$this->productRepository->save($product);
105100
} catch (\Exception $exception) {
106-
throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
101+
throw new CouldNotSaveException(
102+
__('The linked products data is invalid. Verify the data and try again.')
103+
);
107104
}
108105

109106
return true;

0 commit comments

Comments
 (0)