AsynchronousRequestProcessor.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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;
  8. use Magento\Framework\Exception\BulkException;
  9. use Magento\Webapi\Controller\Rest\RequestProcessorInterface;
  10. use Magento\Framework\Webapi\Rest\Response as RestResponse;
  11. use Magento\WebapiAsync\Controller\Rest\Asynchronous\InputParamsResolver;
  12. use Magento\AsynchronousOperations\Model\MassSchedule;
  13. use Magento\AsynchronousOperations\Model\ConfigInterface as WebApiAsyncConfig;
  14. use Magento\Framework\Reflection\DataObjectProcessor;
  15. use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterfaceFactory;
  16. use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterface;
  17. /**
  18. * Responsible for dispatching single and bulk requests.
  19. * Single requests dispatching represented by this class.
  20. * Bulk requests dispatching represented by virtualType of this class.
  21. */
  22. class AsynchronousRequestProcessor implements RequestProcessorInterface
  23. {
  24. const PROCESSOR_PATH = "/^\\/async(\\/V.+)/";
  25. const BULK_PROCESSOR_PATH = "/^\\/async\/bulk(\\/V.+)/";
  26. /**
  27. * @var \Magento\Framework\Webapi\Rest\Response
  28. */
  29. private $response;
  30. /**
  31. * @var \Magento\WebapiAsync\Controller\Rest\Asynchronous\InputParamsResolver
  32. */
  33. private $inputParamsResolver;
  34. /**
  35. * @var MassSchedule
  36. */
  37. private $asyncBulkPublisher;
  38. /**
  39. * @var WebApiAsyncConfig
  40. */
  41. private $webapiAsyncConfig;
  42. /**
  43. * @var \Magento\Framework\Reflection\DataObjectProcessor
  44. */
  45. private $dataObjectProcessor;
  46. /**
  47. * @var AsyncResponseInterfaceFactory
  48. */
  49. private $asyncResponseFactory;
  50. /**
  51. * @var string Regex pattern
  52. */
  53. private $processorPath;
  54. /**
  55. * Initialize dependencies.
  56. *
  57. * @param RestResponse $response
  58. * @param InputParamsResolver $inputParamsResolver
  59. * @param MassSchedule $asyncBulkPublisher
  60. * @param WebApiAsyncConfig $webapiAsyncConfig
  61. * @param DataObjectProcessor $dataObjectProcessor
  62. * @param AsyncResponseInterfaceFactory $asyncResponse
  63. * @param string $processorPath
  64. */
  65. public function __construct(
  66. RestResponse $response,
  67. InputParamsResolver $inputParamsResolver,
  68. MassSchedule $asyncBulkPublisher,
  69. WebApiAsyncConfig $webapiAsyncConfig,
  70. DataObjectProcessor $dataObjectProcessor,
  71. AsyncResponseInterfaceFactory $asyncResponse,
  72. $processorPath = self::PROCESSOR_PATH
  73. ) {
  74. $this->response = $response;
  75. $this->inputParamsResolver = $inputParamsResolver;
  76. $this->asyncBulkPublisher = $asyncBulkPublisher;
  77. $this->webapiAsyncConfig = $webapiAsyncConfig;
  78. $this->dataObjectProcessor = $dataObjectProcessor;
  79. $this->asyncResponseFactory = $asyncResponse;
  80. $this->processorPath = $processorPath;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function process(\Magento\Framework\Webapi\Rest\Request $request)
  86. {
  87. $path = $request->getPathInfo();
  88. $path = preg_replace($this->processorPath, "$1", $path);
  89. $request->setPathInfo(
  90. $path
  91. );
  92. $entitiesParamsArray = $this->inputParamsResolver->resolve();
  93. $topicName = $this->getTopicName($request);
  94. try {
  95. $asyncResponse = $this->asyncBulkPublisher->publishMass(
  96. $topicName,
  97. $entitiesParamsArray
  98. );
  99. } catch (BulkException $bulkException) {
  100. $asyncResponse = $bulkException->getData();
  101. }
  102. $responseData = $this->dataObjectProcessor->buildOutputDataArray(
  103. $asyncResponse,
  104. AsyncResponseInterface::class
  105. );
  106. $this->response->setStatusCode(RestResponse::STATUS_CODE_202)
  107. ->prepareResponse($responseData);
  108. }
  109. /**
  110. * @param \Magento\Framework\Webapi\Rest\Request $request
  111. * @return string
  112. */
  113. private function getTopicName($request)
  114. {
  115. $route = $this->inputParamsResolver->getRoute();
  116. return $this->webapiAsyncConfig->getTopicName(
  117. $route->getRoutePath(),
  118. $request->getHttpMethod()
  119. );
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function canProcess(\Magento\Framework\Webapi\Rest\Request $request)
  125. {
  126. if ($request->getHttpMethod() === \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET) {
  127. return false;
  128. }
  129. if (preg_match($this->processorPath, $request->getPathInfo()) === 1) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * @param \Magento\Framework\Webapi\Rest\Request $request
  136. * @return bool
  137. */
  138. public function isBulk(\Magento\Framework\Webapi\Rest\Request $request)
  139. {
  140. if (preg_match(self::BULK_PROCESSOR_PATH, $request->getPathInfo()) === 1) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. }