Skip to content

Commit c7822ff

Browse files
committed
refactoring
1 parent 25111f0 commit c7822ff

File tree

6 files changed

+185
-145
lines changed

6 files changed

+185
-145
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ private function getExistingData(CategoryInterface $category, int $storeId)
251251
$existingData = array_diff_key($existingData, array_flip(['path', 'level', 'parent_id']));
252252
$existingData['store_id'] = $storeId;
253253

254-
if (is_array($category->getData())) {
255-
$existingData = array_replace($existingData, $category->getData());
254+
if ($category->getData('save_rewrites_history') !== null) {
255+
$existingData['save_rewrites_history'] = $category->getData('save_rewrites_history');
256256
}
257257

258258
return $existingData;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Model;
9+
10+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
11+
12+
class SetSaveRewriteHistory
13+
{
14+
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
15+
16+
/**
17+
* @var RestRequest
18+
*/
19+
private $request;
20+
21+
/**
22+
* @param RestRequest $request
23+
*/
24+
public function __construct(RestRequest $request)
25+
{
26+
$this->request = $request;
27+
}
28+
29+
/**
30+
* Add 'save_rewrites_history' param to the data
31+
*
32+
* @param array $result
33+
* @param string $entityCode
34+
* @param string $type
35+
* @return mixed
36+
*/
37+
public function execute($result, $entityCode, $type)
38+
{
39+
$requestBodyParams = $this->request->getBodyParams();
40+
41+
if ($this->isCustomAttributesExists($requestBodyParams, $entityCode)) {
42+
foreach ($requestBodyParams[$entityCode]['custom_attributes'] as $attribute) {
43+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
44+
foreach ($result as $resultItem) {
45+
if ($resultItem instanceof $type) {
46+
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
47+
break 2;
48+
}
49+
}
50+
break;
51+
}
52+
}
53+
}
54+
55+
return $result;
56+
}
57+
58+
/**
59+
* Check is any custom options exists in data
60+
*
61+
* @param array $requestBodyParams
62+
* @param string $entityCode
63+
* @return bool
64+
*/
65+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
66+
{
67+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
68+
}
69+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Model\Category;
12+
use Magento\CatalogUrlRewrite\Model\SetSaveRewriteHistory;
13+
14+
/**
15+
* Plugin for CategoryInputParamsResolver
16+
*
17+
* Used to modify category data with save_rewrites_history flag
18+
*/
19+
class CategoryInputParamsResolver
20+
{
21+
/**
22+
* @var SetSaveRewriteHistory
23+
*/
24+
private $rewriteHistory;
25+
26+
/**
27+
* @param SetSaveRewriteHistory $rewriteHistory
28+
*/
29+
public function __construct(SetSaveRewriteHistory $rewriteHistory)
30+
{
31+
$this->rewriteHistory = $rewriteHistory;
32+
}
33+
34+
/**
35+
* Add 'save_rewrites_history' param to the category data
36+
*
37+
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper
38+
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
39+
* @param array $result
40+
* @return array
41+
*/
42+
public function afterResolve(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): array
43+
{
44+
$route = $subject->getRoute();
45+
46+
if ($route->getServiceClass() === CategoryRepositoryInterface::class && $route->getServiceMethod() === 'save') {
47+
$result = $this->rewriteHistory->execute(
48+
$result,
49+
'category',
50+
Category::class
51+
);
52+
}
53+
54+
return $result;
55+
}
56+
}

app/code/Magento/CatalogUrlRewrite/Plugin/Webapi/Controller/Rest/InputParamsResolver.php

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\CatalogUrlRewrite\Model\SetSaveRewriteHistory;
13+
14+
/**
15+
* Plugin for InputParamsResolver
16+
*
17+
* Used to modify product data with save_rewrites_history flag
18+
*/
19+
class ProductInputParamsResolver
20+
{
21+
/**
22+
* @var SetSaveRewriteHistory
23+
*/
24+
private $rewriteHistory;
25+
26+
/**
27+
* @param SetSaveRewriteHistory $rewriteHistory
28+
*/
29+
public function __construct(SetSaveRewriteHistory $rewriteHistory)
30+
{
31+
$this->rewriteHistory = $rewriteHistory;
32+
}
33+
34+
/**
35+
* Add 'save_rewrites_history' param to the product data
36+
*
37+
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper
38+
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
39+
* @param array $result
40+
* @return array
41+
*/
42+
public function afterResolve(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): array
43+
{
44+
$route = $subject->getRoute();
45+
46+
if ($route->getServiceClass() === ProductRepositoryInterface::class && $route->getServiceMethod() === 'save') {
47+
$result = $this->rewriteHistory->execute(
48+
$result,
49+
'product',
50+
Product::class
51+
);
52+
}
53+
54+
return $result;
55+
}
56+
}

app/code/Magento/CatalogUrlRewrite/etc/webapi_rest/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<type name="Magento\Webapi\Controller\Rest\InputParamsResolver">
10-
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver" sortOrder="1" disabled="false" />
10+
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\ProductInputParamsResolver" disabled="false" />
11+
<plugin name="category_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\CategoryInputParamsResolver" disabled="false" />
1112
</type>
1213
</config>

0 commit comments

Comments
 (0)