|
6 | 6 |
|
7 | 7 | namespace Magento\Catalog\Model\ProductLink;
|
8 | 8 |
|
9 |
| -use Magento\Catalog\Api\Data; |
10 | 9 | use Magento\Framework\Exception\CouldNotSaveException;
|
11 | 10 | use Magento\Framework\Exception\NoSuchEntityException;
|
12 | 11 | use Magento\Framework\Exception\InputException;
|
| 12 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 13 | +use Magento\Catalog\Model\Product\LinkTypeProvider; |
| 14 | +use Magento\Catalog\Api\ProductLinkManagementInterface; |
13 | 15 |
|
14 |
| -class Management implements \Magento\Catalog\Api\ProductLinkManagementInterface |
| 16 | +class Management implements ProductLinkManagementInterface |
15 | 17 | {
|
16 | 18 | /**
|
17 |
| - * @var \Magento\Catalog\Api\ProductRepositoryInterface |
| 19 | + * @var ProductRepositoryInterface |
18 | 20 | */
|
19 | 21 | protected $productRepository;
|
20 | 22 |
|
21 | 23 | /**
|
22 |
| - * @var \Magento\Catalog\Model\Product\LinkTypeProvider |
| 24 | + * @var LinkTypeProvider |
23 | 25 | */
|
24 | 26 | protected $linkTypeProvider;
|
25 | 27 |
|
26 | 28 | /**
|
27 |
| - * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository |
28 |
| - * @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider |
| 29 | + * @param ProductRepositoryInterface $productRepository |
| 30 | + * @param LinkTypeProvider $linkTypeProvider |
29 | 31 | */
|
30 | 32 | public function __construct(
|
31 |
| - \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, |
32 |
| - \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider |
| 33 | + ProductRepositoryInterface $productRepository, |
| 34 | + LinkTypeProvider $linkTypeProvider |
33 | 35 | ) {
|
34 | 36 | $this->productRepository = $productRepository;
|
35 | 37 | $this->linkTypeProvider = $linkTypeProvider;
|
@@ -67,43 +69,38 @@ public function getLinkedItemsByType($sku, $type)
|
67 | 69 | */
|
68 | 70 | public function setProductLinks($sku, array $items)
|
69 | 71 | {
|
| 72 | + |
| 73 | + if (empty($items)) { |
| 74 | + throw InputException::invalidFieldValue('items', 'empty array'); |
| 75 | + } |
| 76 | + |
70 | 77 | $linkTypes = $this->linkTypeProvider->getLinkTypes();
|
71 | 78 |
|
72 | 79 | // 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 | + ); |
84 | 89 | }
|
85 | 90 | }
|
86 | 91 |
|
87 | 92 | $product = $this->productRepository->get($sku);
|
88 | 93 |
|
89 |
| - // Replace only links of the specified type |
90 | 94 | $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 | + |
102 | 97 | $product->setProductLinks($newLinks);
|
103 | 98 | try {
|
104 | 99 | $this->productRepository->save($product);
|
105 | 100 | } 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 | + ); |
107 | 104 | }
|
108 | 105 |
|
109 | 106 | return true;
|
|
0 commit comments