Skip to content

Commit 99da072

Browse files
committed
#29174 Fix complexity
1 parent 79c54ae commit 99da072

File tree

1 file changed

+62
-39
lines changed

1 file changed

+62
-39
lines changed

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

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Api\CategoryRepositoryInterface;
1212
use Magento\Catalog\Api\ProductRepositoryInterface;
1313
use Magento\Framework\Webapi\Rest\Request as RestRequest;
14+
use Magento\Webapi\Controller\Rest\Router\Route;
1415

1516
/**
1617
* Plugin for InputParamsResolver
@@ -44,64 +45,37 @@ public function __construct(RestRequest $request)
4445
*/
4546
public function afterResolve(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): array
4647
{
47-
$route = $subject->getRoute();
48-
$serviceMethodName = $route->getServiceMethod();
49-
$serviceClassName = $route->getServiceClass();
50-
$requestBodyParams = $this->request->getBodyParams();
48+
$this->processProductCall($subject, $result);
49+
$this->processCategoryCall($subject, $result);
5150

52-
if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName)
53-
&& $this->isCustomAttributesExists($requestBodyParams, 'product')) {
54-
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
55-
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
56-
foreach ($result as $resultItem) {
57-
if ($resultItem instanceof \Magento\Catalog\Model\Product) {
58-
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
59-
break 2;
60-
}
61-
}
62-
break;
63-
}
64-
}
65-
}
66-
67-
if ($this->isCategorySaveCalled($serviceClassName, $serviceMethodName)
68-
&& $this->isCustomAttributesExists($requestBodyParams, 'category')) {
69-
foreach ($requestBodyParams['category']['custom_attributes'] as $attribute) {
70-
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
71-
foreach ($result as $resultItem) {
72-
if ($resultItem instanceof \Magento\Catalog\Model\Category) {
73-
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
74-
break 2;
75-
}
76-
}
77-
break;
78-
}
79-
}
80-
}
8151
return $result;
8252
}
8353

8454
/**
8555
* Check that product save method called
8656
*
87-
* @param string $serviceClassName
88-
* @param string $serviceMethodName
57+
* @param Route $route
8958
* @return bool
9059
*/
91-
private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool
60+
private function isProductSaveCalled(Route $route): bool
9261
{
62+
$serviceMethodName = $route->getServiceMethod();
63+
$serviceClassName = $route->getServiceClass();
64+
9365
return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save';
9466
}
9567

9668
/**
9769
* Check that category save method called
9870
*
99-
* @param string $serviceClassName
100-
* @param string $serviceMethodName
71+
* @param Route $route
10172
* @return bool
10273
*/
103-
private function isCategorySaveCalled(string $serviceClassName, string $serviceMethodName): bool
74+
private function isCategorySaveCalled(Route $route): bool
10475
{
76+
$serviceMethodName = $route->getServiceMethod();
77+
$serviceClassName = $route->getServiceClass();
78+
10579
return $serviceClassName === CategoryRepositoryInterface::class && $serviceMethodName === 'save';
10680
}
10781

@@ -116,4 +90,53 @@ private function isCustomAttributesExists(array $requestBodyParams, string $enti
11690
{
11791
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
11892
}
93+
94+
/**
95+
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
96+
* @param array $result
97+
* @return array
98+
*/
99+
private function processProductCall(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): void
100+
{
101+
$requestBodyParams = $this->request->getBodyParams();
102+
103+
if ($this->isProductSaveCalled($subject->getRoute())
104+
&& $this->isCustomAttributesExists($requestBodyParams, 'product')) {
105+
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
106+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
107+
foreach ($result as $resultItem) {
108+
if ($resultItem instanceof \Magento\Catalog\Model\Product) {
109+
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
110+
break 2;
111+
}
112+
}
113+
break;
114+
}
115+
}
116+
}
117+
}
118+
119+
/**
120+
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
121+
* @param array $result
122+
*/
123+
private function processCategoryCall(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): void
124+
{
125+
$requestBodyParams = $this->request->getBodyParams();
126+
127+
if ($this->isCategorySaveCalled($subject->getRoute())
128+
&& $this->isCustomAttributesExists($requestBodyParams, 'category')) {
129+
foreach ($requestBodyParams['category']['custom_attributes'] as $attribute) {
130+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
131+
foreach ($result as $resultItem) {
132+
if ($resultItem instanceof \Magento\Catalog\Model\Category) {
133+
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
134+
break 2;
135+
}
136+
}
137+
break;
138+
}
139+
}
140+
}
141+
}
119142
}

0 commit comments

Comments
 (0)