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