123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\WebapiAsync\Controller\Rest\Asynchronous;
- use Magento\Framework\Webapi\ServiceInputProcessor;
- use Magento\Framework\Webapi\Rest\Request as RestRequest;
- use Magento\Webapi\Controller\Rest\Router;
- use Magento\Webapi\Controller\Rest\ParamsOverrider;
- use Magento\Webapi\Controller\Rest\RequestValidator;
- use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver;
- /**
- * This class is responsible for retrieving resolved input data
- */
- class InputParamsResolver
- {
- /**
- * @var RestRequest
- */
- private $request;
- /**
- * @var ParamsOverrider
- */
- private $paramsOverrider;
- /**
- * @var ServiceInputProcessor
- */
- private $serviceInputProcessor;
- /**
- * @var Router
- */
- private $router;
- /**
- * @var RequestValidator
- */
- private $requestValidator;
- /**
- * @var \Magento\Webapi\Controller\Rest\InputParamsResolver
- */
- private $inputParamsResolver;
- /**
- * @var bool
- */
- private $isBulk;
- /**
- * Initialize dependencies.
- *
- * @param \Magento\Framework\Webapi\Rest\Request $request
- * @param \Magento\Webapi\Controller\Rest\ParamsOverrider $paramsOverrider
- * @param \Magento\Framework\Webapi\ServiceInputProcessor $inputProcessor
- * @param \Magento\Webapi\Controller\Rest\Router $router
- * @param \Magento\Webapi\Controller\Rest\RequestValidator $requestValidator
- * @param \Magento\Webapi\Controller\Rest\InputParamsResolver $inputParamsResolver
- * @param bool $isBulk
- */
- public function __construct(
- RestRequest $request,
- ParamsOverrider $paramsOverrider,
- ServiceInputProcessor $inputProcessor,
- Router $router,
- RequestValidator $requestValidator,
- WebapiInputParamsResolver $inputParamsResolver,
- $isBulk = false
- ) {
- $this->request = $request;
- $this->paramsOverrider = $paramsOverrider;
- $this->serviceInputProcessor = $inputProcessor;
- $this->router = $router;
- $this->requestValidator = $requestValidator;
- $this->inputParamsResolver = $inputParamsResolver;
- $this->isBulk = $isBulk;
- }
- /**
- * Process and resolve input parameters
- *
- * Return array with validated input params
- * or throw \Exception if at least one request entity params is not valid
- *
- * @return array
- * @throws \Magento\Framework\Exception\InputException if no value is provided for required parameters
- * @throws \Magento\Framework\Webapi\Exception
- * @throws \Magento\Framework\Exception\AuthorizationException
- */
- public function resolve()
- {
- if ($this->isBulk === false) {
- return [$this->inputParamsResolver->resolve()];
- }
- $this->requestValidator->validate();
- $webapiResolvedParams = [];
- $inputData = $this->request->getRequestData();
- $httpMethod = $this->request->getHttpMethod();
- if ($httpMethod == \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE) {
- $requestBodyParams = $this->request->getBodyParams();
- $inputData = array_merge($requestBodyParams, $inputData);
- }
- foreach ($inputData as $key => $singleEntityParams) {
- $webapiResolvedParams[$key] = $this->resolveBulkItemParams($singleEntityParams);
- }
- return $webapiResolvedParams;
- }
- /**
- * Returns route.
- *
- * @return \Magento\Webapi\Controller\Rest\Router\Route
- */
- public function getRoute()
- {
- return $this->inputParamsResolver->getRoute();
- }
- /**
- * Resolve parameters for service
- *
- * Convert the input array from key-value format to a list of parameters
- * suitable for the specified class / method.
- *
- * Instead of \Magento\Webapi\Controller\Rest\InputParamsResolver
- * we don't need to merge body params with url params and use only body params
- *
- * @param array $inputData data to send to method in key-value format
- * @return array list of parameters that can be used to call the service method
- * @throws \Magento\Framework\Exception\InputException if no value is provided for required parameters
- * @throws \Magento\Framework\Webapi\Exception
- */
- private function resolveBulkItemParams($inputData)
- {
- $route = $this->getRoute();
- $serviceMethodName = $route->getServiceMethod();
- $serviceClassName = $route->getServiceClass();
- $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
- return $inputParams;
- }
- }
|