Skip to content

Commit

Permalink
#29174 Fix complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
amenk committed Jul 25, 2020
1 parent 79c54ae commit 99da072
Showing 1 changed file with 62 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Webapi\Controller\Rest\Router\Route;

/**
* Plugin for InputParamsResolver
Expand Down Expand Up @@ -44,64 +45,37 @@ public function __construct(RestRequest $request)
*/
public function afterResolve(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): array
{
$route = $subject->getRoute();
$serviceMethodName = $route->getServiceMethod();
$serviceClassName = $route->getServiceClass();
$requestBodyParams = $this->request->getBodyParams();
$this->processProductCall($subject, $result);
$this->processCategoryCall($subject, $result);

if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName)
&& $this->isCustomAttributesExists($requestBodyParams, 'product')) {
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
foreach ($result as $resultItem) {
if ($resultItem instanceof \Magento\Catalog\Model\Product) {
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
break 2;
}
}
break;
}
}
}

if ($this->isCategorySaveCalled($serviceClassName, $serviceMethodName)
&& $this->isCustomAttributesExists($requestBodyParams, 'category')) {
foreach ($requestBodyParams['category']['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
foreach ($result as $resultItem) {
if ($resultItem instanceof \Magento\Catalog\Model\Category) {
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
break 2;
}
}
break;
}
}
}
return $result;
}

/**
* Check that product save method called
*
* @param string $serviceClassName
* @param string $serviceMethodName
* @param Route $route
* @return bool
*/
private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool
private function isProductSaveCalled(Route $route): bool
{
$serviceMethodName = $route->getServiceMethod();
$serviceClassName = $route->getServiceClass();

return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save';
}

/**
* Check that category save method called
*
* @param string $serviceClassName
* @param string $serviceMethodName
* @param Route $route
* @return bool
*/
private function isCategorySaveCalled(string $serviceClassName, string $serviceMethodName): bool
private function isCategorySaveCalled(Route $route): bool
{
$serviceMethodName = $route->getServiceMethod();
$serviceClassName = $route->getServiceClass();

return $serviceClassName === CategoryRepositoryInterface::class && $serviceMethodName === 'save';
}

Expand All @@ -116,4 +90,53 @@ private function isCustomAttributesExists(array $requestBodyParams, string $enti
{
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
}

/**
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
* @param array $result
* @return array
*/
private function processProductCall(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): void
{
$requestBodyParams = $this->request->getBodyParams();

if ($this->isProductSaveCalled($subject->getRoute())
&& $this->isCustomAttributesExists($requestBodyParams, 'product')) {
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
foreach ($result as $resultItem) {
if ($resultItem instanceof \Magento\Catalog\Model\Product) {
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
break 2;
}
}
break;
}
}
}
}

/**
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
* @param array $result
*/
private function processCategoryCall(\Magento\Webapi\Controller\Rest\InputParamsResolver $subject, array $result): void
{
$requestBodyParams = $this->request->getBodyParams();

if ($this->isCategorySaveCalled($subject->getRoute())
&& $this->isCustomAttributesExists($requestBodyParams, 'category')) {
foreach ($requestBodyParams['category']['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
foreach ($result as $resultItem) {
if ($resultItem instanceof \Magento\Catalog\Model\Category) {
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
break 2;
}
}
break;
}
}
}
}
}

0 comments on commit 99da072

Please sign in to comment.