Skip to content

Commit

Permalink
ENGCOM-6983: #26800 Fixed Undefined variable in ProductLink/Management
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Feb 26, 2020
2 parents 0053c20 + d26a2b7 commit 260dd64
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ProductLinkManagementInterface
*
* @param string $sku
* @param string $type
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
*/
public function getLinkedItemsByType($sku, $type);
Expand All @@ -28,6 +29,7 @@ public function getLinkedItemsByType($sku, $type);
* @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $items
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @return bool
*/
public function setProductLinks($sku, array $items);
Expand Down
68 changes: 34 additions & 34 deletions app/code/Magento/Catalog/Model/ProductLink/Management.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,42 @@

namespace Magento\Catalog\Model\ProductLink;

use Magento\Catalog\Api\Data;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\LinkTypeProvider;
use Magento\Catalog\Api\ProductLinkManagementInterface;

class Management implements \Magento\Catalog\Api\ProductLinkManagementInterface
/**
* Manage product links from api
*/
class Management implements ProductLinkManagementInterface
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
* @var ProductRepositoryInterface
*/
protected $productRepository;

/**
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
* @var LinkTypeProvider
*/
protected $linkTypeProvider;

/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param ProductRepositoryInterface $productRepository
* @param LinkTypeProvider $linkTypeProvider
*/
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
ProductRepositoryInterface $productRepository,
LinkTypeProvider $linkTypeProvider
) {
$this->productRepository = $productRepository;
$this->linkTypeProvider = $linkTypeProvider;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getLinkedItemsByType($sku, $type)
{
Expand All @@ -63,47 +68,42 @@ public function getLinkedItemsByType($sku, $type)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setProductLinks($sku, array $items)
{

if (empty($items)) {
throw InputException::invalidFieldValue('items', 'empty array');
}

$linkTypes = $this->linkTypeProvider->getLinkTypes();

// Check if product link type is set and correct
if (!empty($items)) {
foreach ($items as $newLink) {
$type = $newLink->getLinkType();
if ($type == null) {
throw InputException::requiredField("linkType");
}
if (!isset($linkTypes[$type])) {
throw new NoSuchEntityException(
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
);
}
foreach ($items as $newLink) {
$type = $newLink->getLinkType();
if ($type == null) {
throw InputException::requiredField("linkType");
}
if (!isset($linkTypes[$type])) {
throw new NoSuchEntityException(
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
);
}
}

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

// Replace only links of the specified type
$existingLinks = $product->getProductLinks();
$newLinks = [];
if (!empty($existingLinks)) {
foreach ($existingLinks as $link) {
if ($link->getLinkType() != $type) {
$newLinks[] = $link;
}
}
$newLinks = array_merge($newLinks, $items);
} else {
$newLinks = $items;
}
$newLinks = array_merge($existingLinks, $items);

$product->setProductLinks($newLinks);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
throw new CouldNotSaveException(
__('The linked products data is invalid. Verify the data and try again.')
);
}

return true;
Expand Down
Loading

0 comments on commit 260dd64

Please sign in to comment.