123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?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;
- }
- }
|