Skip to content

Commit 3558184

Browse files
committed
refactor getByIds method and use object manager into constructor class
1 parent c5eb623 commit 3558184

File tree

5 files changed

+16
-59
lines changed

5 files changed

+16
-59
lines changed

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,15 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
3737
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink);
3838

3939
/**
40-
* Remove the product assignment from the category by category id and array of sku
40+
* Remove the product assignment from the category by category id and array or string of sku
4141
*
4242
* @param int $categoryId
43-
* @param array $sku
43+
* @param string|array $sku
4444
* @return bool will returned True if products successfully deleted
4545
*
4646
* @throws \Magento\Framework\Exception\CouldNotSaveException
4747
* @throws \Magento\Framework\Exception\StateException
4848
* @throws \Magento\Framework\Exception\InputException
4949
*/
5050
public function deleteByIds($categoryId, $sku);
51-
52-
/**
53-
* Remove the product assignment from the category by category id and sku
54-
*
55-
* @param int $categoryId
56-
* @param string $sku
57-
* @return bool will returned True if products successfully deleted
58-
*
59-
* @throws \Magento\Framework\Exception\CouldNotSaveException
60-
* @throws \Magento\Framework\Exception\StateException
61-
* @throws \Magento\Framework\Exception\InputException
62-
*/
63-
public function deleteById($categoryId, $sku);
6451
}

app/code/Magento/Catalog/Model/CategoryLinkManagement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function assignProductToCategories($productSku, array $categoryIds)
9393
$product = $this->getProductRepository()->get($productSku);
9494
$assignedCategories = $this->getProductResource()->getCategoryIds($product);
9595
foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
96-
$this->getCategoryLinkRepository()->deleteById($categoryId, $productSku);
96+
$this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku);
9797
}
9898

9999
foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {

app/code/Magento/Catalog/Model/CategoryLinkRepository.php

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkReposit
2929
/**
3030
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
3131
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
32-
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource
3332
*/
3433
public function __construct(
3534
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
36-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
37-
\Magento\Catalog\Model\ResourceModel\Product $productResource
35+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
3836
) {
3937
$this->categoryRepository = $categoryRepository;
4038
$this->productRepository = $productRepository;
41-
$this->productResource = $productResource;
39+
$this->productResource = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Model\ResourceModel\Product::class);
4240
}
4341

4442
/**
@@ -72,17 +70,24 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
7270
*/
7371
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
7472
{
75-
return $this->deleteById($productLink->getCategoryId(), $productLink->getSku());
73+
return $this->deleteByIds($productLink->getCategoryId(), $productLink->getSku());
7674
}
7775

7876
/**
7977
* {@inheritdoc}
8078
*/
8179
public function deleteByIds($categoryId, $sku)
8280
{
81+
if (!is_array($sku)) {
82+
$sku = [$sku];
83+
}
8384
$category = $this->categoryRepository->get($categoryId);
8485
$products = $this->productResource->getProductsIdsBySkus($sku);
8586

87+
if (!$products) {
88+
throw new InputException(__("The category doesn't contain the specified products."));
89+
}
90+
8691
$productPositions = $category->getProductsPosition();
8792

8893
foreach ($products as $productSku => $productId) {
@@ -108,39 +113,4 @@ public function deleteByIds($categoryId, $sku)
108113
}
109114
return true;
110115
}
111-
112-
/**
113-
* {@inheritDoc}
114-
*/
115-
public function deleteById($categoryId, $sku)
116-
{
117-
$category = $this->categoryRepository->get($categoryId);
118-
$product = $this->productRepository->get($sku);
119-
$productPositions = $category->getProductsPosition();
120-
121-
$productID = $product->getId();
122-
if (!isset($productPositions[$productID])) {
123-
throw new InputException(__("The category doesn't contain the specified product."));
124-
}
125-
$backupPosition = $productPositions[$productID];
126-
unset($productPositions[$productID]);
127-
128-
$category->setPostedProducts($productPositions);
129-
try {
130-
$category->save();
131-
} catch (\Exception $e) {
132-
throw new CouldNotSaveException(
133-
__(
134-
'Could not save product "%product" with position %position to category %category',
135-
[
136-
"product" => $product->getId(),
137-
"position" => $backupPosition,
138-
"category" => $category->getId()
139-
]
140-
),
141-
$e
142-
);
143-
}
144-
return true;
145-
}
146116
}

app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkRepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testDeleteByIds()
113113
$productMock->expects($this->once())->method('getId')->willReturn($productId);
114114
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
115115
$categoryMock->expects($this->once())->method('save');
116-
$this->assertTrue($this->model->deleteById($categoryId, $productSku));
116+
$this->assertTrue($this->model->deleteByIds($categoryId, $productSku));
117117
}
118118

119119
/**
@@ -140,7 +140,7 @@ public function testDeleteByIdsWithCouldNotSaveException()
140140
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
141141
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
142142
$categoryMock->expects($this->once())->method('save')->willThrowException(new \Exception());
143-
$this->model->deleteById($categoryId, $productSku);
143+
$this->model->deleteByIds($categoryId, $productSku);
144144
}
145145

146146
/**

app/code/Magento/Catalog/etc/webapi.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
</resources>
458458
</route>
459459
<route url="/V1/categories/:categoryId/products/:sku" method="DELETE">
460-
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteById" />
460+
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteByIds" />
461461
<resources>
462462
<resource ref="Magento_Catalog::categories" />
463463
</resources>

0 commit comments

Comments
 (0)