|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Webapi\Rest\Request as RestRequest; |
| 13 | + |
| 14 | +/** |
| 15 | + * Plugin for InputParamsResolver |
| 16 | + * |
| 17 | + * Used to modify product data with save_rewrites_history flag |
| 18 | + */ |
| 19 | +class InputParamsResolver |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var RestRequest |
| 23 | + */ |
| 24 | + private $request; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param RestRequest $request |
| 28 | + */ |
| 29 | + public function __construct(RestRequest $request) |
| 30 | + { |
| 31 | + $this->request = $request; |
| 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 | + $serviceMethodName = $route->getServiceMethod(); |
| 46 | + $serviceClassName = $route->getServiceClass(); |
| 47 | + $requestBodyParams = $this->request->getBodyParams(); |
| 48 | + |
| 49 | + if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName) |
| 50 | + && $this->isCustomAttributesExists($requestBodyParams)) { |
| 51 | + foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) { |
| 52 | + if ($attribute['attribute_code'] === 'save_rewrites_history') { |
| 53 | + foreach ($result as $resultItem) { |
| 54 | + if ($resultItem instanceof \Magento\Catalog\Model\Product) { |
| 55 | + $resultItem->setData('save_rewrites_history', (bool)$attribute['value']); |
| 56 | + break 2; |
| 57 | + } |
| 58 | + } |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return $result; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Check that product save method called |
| 68 | + * |
| 69 | + * @param string $serviceClassName |
| 70 | + * @param string $serviceMethodName |
| 71 | + * @return bool |
| 72 | + */ |
| 73 | + private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool |
| 74 | + { |
| 75 | + return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save'; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Check is any custom options exists in product data |
| 80 | + * |
| 81 | + * @param array $requestBodyParams |
| 82 | + * @return bool |
| 83 | + */ |
| 84 | + private function isCustomAttributesExists(array $requestBodyParams): bool |
| 85 | + { |
| 86 | + return !empty($requestBodyParams['product']['custom_attributes']); |
| 87 | + } |
| 88 | +} |
0 commit comments