InputParamsResolver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Controller\Rest\Asynchronous;
  8. use Magento\Framework\Webapi\ServiceInputProcessor;
  9. use Magento\Framework\Webapi\Rest\Request as RestRequest;
  10. use Magento\Webapi\Controller\Rest\Router;
  11. use Magento\Webapi\Controller\Rest\ParamsOverrider;
  12. use Magento\Webapi\Controller\Rest\RequestValidator;
  13. use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver;
  14. /**
  15. * This class is responsible for retrieving resolved input data
  16. */
  17. class InputParamsResolver
  18. {
  19. /**
  20. * @var RestRequest
  21. */
  22. private $request;
  23. /**
  24. * @var ParamsOverrider
  25. */
  26. private $paramsOverrider;
  27. /**
  28. * @var ServiceInputProcessor
  29. */
  30. private $serviceInputProcessor;
  31. /**
  32. * @var Router
  33. */
  34. private $router;
  35. /**
  36. * @var RequestValidator
  37. */
  38. private $requestValidator;
  39. /**
  40. * @var \Magento\Webapi\Controller\Rest\InputParamsResolver
  41. */
  42. private $inputParamsResolver;
  43. /**
  44. * @var bool
  45. */
  46. private $isBulk;
  47. /**
  48. * Initialize dependencies.
  49. *
  50. * @param \Magento\Framework\Webapi\Rest\Request $request
  51. * @param \Magento\Webapi\Controller\Rest\ParamsOverrider $paramsOverrider
  52. * @param \Magento\Framework\Webapi\ServiceInputProcessor $inputProcessor
  53. * @param \Magento\Webapi\Controller\Rest\Router $router
  54. * @param \Magento\Webapi\Controller\Rest\RequestValidator $requestValidator
  55. * @param \Magento\Webapi\Controller\Rest\InputParamsResolver $inputParamsResolver
  56. * @param bool $isBulk
  57. */
  58. public function __construct(
  59. RestRequest $request,
  60. ParamsOverrider $paramsOverrider,
  61. ServiceInputProcessor $inputProcessor,
  62. Router $router,
  63. RequestValidator $requestValidator,
  64. WebapiInputParamsResolver $inputParamsResolver,
  65. $isBulk = false
  66. ) {
  67. $this->request = $request;
  68. $this->paramsOverrider = $paramsOverrider;
  69. $this->serviceInputProcessor = $inputProcessor;
  70. $this->router = $router;
  71. $this->requestValidator = $requestValidator;
  72. $this->inputParamsResolver = $inputParamsResolver;
  73. $this->isBulk = $isBulk;
  74. }
  75. /**
  76. * Process and resolve input parameters
  77. *
  78. * Return array with validated input params
  79. * or throw \Exception if at least one request entity params is not valid
  80. *
  81. * @return array
  82. * @throws \Magento\Framework\Exception\InputException if no value is provided for required parameters
  83. * @throws \Magento\Framework\Webapi\Exception
  84. * @throws \Magento\Framework\Exception\AuthorizationException
  85. */
  86. public function resolve()
  87. {
  88. if ($this->isBulk === false) {
  89. return [$this->inputParamsResolver->resolve()];
  90. }
  91. $this->requestValidator->validate();
  92. $webapiResolvedParams = [];
  93. $inputData = $this->request->getRequestData();
  94. $httpMethod = $this->request->getHttpMethod();
  95. if ($httpMethod == \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE) {
  96. $requestBodyParams = $this->request->getBodyParams();
  97. $inputData = array_merge($requestBodyParams, $inputData);
  98. }
  99. foreach ($inputData as $key => $singleEntityParams) {
  100. $webapiResolvedParams[$key] = $this->resolveBulkItemParams($singleEntityParams);
  101. }
  102. return $webapiResolvedParams;
  103. }
  104. /**
  105. * Returns route.
  106. *
  107. * @return \Magento\Webapi\Controller\Rest\Router\Route
  108. */
  109. public function getRoute()
  110. {
  111. return $this->inputParamsResolver->getRoute();
  112. }
  113. /**
  114. * Resolve parameters for service
  115. *
  116. * Convert the input array from key-value format to a list of parameters
  117. * suitable for the specified class / method.
  118. *
  119. * Instead of \Magento\Webapi\Controller\Rest\InputParamsResolver
  120. * we don't need to merge body params with url params and use only body params
  121. *
  122. * @param array $inputData data to send to method in key-value format
  123. * @return array list of parameters that can be used to call the service method
  124. * @throws \Magento\Framework\Exception\InputException if no value is provided for required parameters
  125. * @throws \Magento\Framework\Webapi\Exception
  126. */
  127. private function resolveBulkItemParams($inputData)
  128. {
  129. $route = $this->getRoute();
  130. $serviceMethodName = $route->getServiceMethod();
  131. $serviceClassName = $route->getServiceClass();
  132. $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
  133. return $inputParams;
  134. }
  135. }