Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AsynchronousRequestProcessor.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
18 use Magento\AsynchronousOperations\Api\Data\AsyncResponseInterfaceFactory;
20 
27 {
28  const PROCESSOR_PATH = "/^\\/async(\\/V.+)/";
29  const BULK_PROCESSOR_PATH = "/^\\/async\/bulk(\\/V.+)/";
30 
34  private $response;
38  private $inputParamsResolver;
42  private $asyncBulkPublisher;
46  private $webapiAsyncConfig;
50  private $dataObjectProcessor;
54  private $asyncResponseFactory;
58  private $processorPath;
59 
71  public function __construct(
72  RestResponse $response,
73  InputParamsResolver $inputParamsResolver,
74  MassSchedule $asyncBulkPublisher,
75  WebApiAsyncConfig $webapiAsyncConfig,
76  DataObjectProcessor $dataObjectProcessor,
77  AsyncResponseInterfaceFactory $asyncResponse,
78  $processorPath = self::PROCESSOR_PATH
79  ) {
80  $this->response = $response;
81  $this->inputParamsResolver = $inputParamsResolver;
82  $this->asyncBulkPublisher = $asyncBulkPublisher;
83  $this->webapiAsyncConfig = $webapiAsyncConfig;
84  $this->dataObjectProcessor = $dataObjectProcessor;
85  $this->asyncResponseFactory = $asyncResponse;
86  $this->processorPath = $processorPath;
87  }
88 
92  public function process(\Magento\Framework\Webapi\Rest\Request $request)
93  {
94  $path = $request->getPathInfo();
95  $path = preg_replace($this->processorPath, "$1", $path);
96  $request->setPathInfo(
97  $path
98  );
99 
100  $entitiesParamsArray = $this->inputParamsResolver->resolve();
101  $topicName = $this->getTopicName($request);
102 
103  try {
104  $asyncResponse = $this->asyncBulkPublisher->publishMass(
105  $topicName,
106  $entitiesParamsArray
107  );
108  } catch (BulkException $bulkException) {
109  $asyncResponse = $bulkException->getData();
110  }
111 
112  $responseData = $this->dataObjectProcessor->buildOutputDataArray(
113  $asyncResponse,
114  AsyncResponseInterface::class
115  );
116 
117  $this->response->setStatusCode(RestResponse::STATUS_CODE_202)
118  ->prepareResponse($responseData);
119  }
120 
125  private function getTopicName($request)
126  {
127  $route = $this->inputParamsResolver->getRoute();
128 
129  return $this->webapiAsyncConfig->getTopicName(
130  $route->getRoutePath(),
131  $request->getHttpMethod()
132  );
133  }
134 
138  public function canProcess(\Magento\Framework\Webapi\Rest\Request $request)
139  {
141  return false;
142  }
143 
144  if (preg_match($this->processorPath, $request->getPathInfo()) === 1) {
145  return true;
146  }
147  return false;
148  }
149 
154  public function isBulk(\Magento\Framework\Webapi\Rest\Request $request)
155  {
156  if (preg_match(self::BULK_PROCESSOR_PATH, $request->getPathInfo()) === 1) {
157  return true;
158  }
159  return false;
160  }
161 }
$response
Definition: 404.php:11
__construct(RestResponse $response, InputParamsResolver $inputParamsResolver, MassSchedule $asyncBulkPublisher, WebApiAsyncConfig $webapiAsyncConfig, DataObjectProcessor $dataObjectProcessor, AsyncResponseInterfaceFactory $asyncResponse, $processorPath=self::PROCESSOR_PATH)