Skip to content

Commit cb4969c

Browse files
committed
Merge remote-tracking branch 'origin/MC-31878' into 2.4-develop-pr17
2 parents 05df494 + 76e022a commit cb4969c

File tree

9 files changed

+387
-24
lines changed

9 files changed

+387
-24
lines changed

app/code/Magento/AsynchronousOperations/Model/MassSchedule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MassSchedule
5353
private $logger;
5454

5555
/**
56-
* @var OperationRepository
56+
* @var OperationRepositoryInterface
5757
*/
5858
private $operationRepository;
5959

@@ -75,7 +75,7 @@ class MassSchedule
7575
* @param AsyncResponseInterfaceFactory $asyncResponseFactory
7676
* @param BulkManagementInterface $bulkManagement
7777
* @param LoggerInterface $logger
78-
* @param OperationRepository $operationRepository
78+
* @param OperationRepositoryInterface $operationRepository
7979
* @param UserContextInterface $userContext
8080
* @param Encryptor $encryptor
8181
*/
@@ -85,7 +85,7 @@ public function __construct(
8585
AsyncResponseInterfaceFactory $asyncResponseFactory,
8686
BulkManagementInterface $bulkManagement,
8787
LoggerInterface $logger,
88-
OperationRepository $operationRepository,
88+
OperationRepositoryInterface $operationRepository,
8989
UserContextInterface $userContext,
9090
Encryptor $encryptor
9191
) {
@@ -137,7 +137,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
137137
$requestItem = $this->itemStatusInterfaceFactory->create();
138138

139139
try {
140-
$operation = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
140+
$operation = $this->operationRepository->create($topicName, $entityParams, $groupId, $key);
141141
$operations[] = $operation;
142142
$requestItem->setId($key);
143143
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AsynchronousOperations\Model;
9+
10+
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
11+
12+
/**
13+
* Repository interface to create operation
14+
*/
15+
interface OperationRepositoryInterface
16+
{
17+
/**
18+
* Create operation by topic, parameters and group ID
19+
*
20+
* @param string $topicName
21+
* @param array $entityParams
22+
* format: array(
23+
* '<arg1-name>' => '<arg1-value>',
24+
* '<arg2-name>' => '<arg2-value>',
25+
* )
26+
* @param string $groupId
27+
* @param int $operationId
28+
* @return OperationInterface
29+
*/
30+
public function create($topicName, $entityParams, $groupId, $operationId): OperationInterface;
31+
}

app/code/Magento/AsynchronousOperations/Model/ResourceModel/Operation/OperationRepository.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
1212
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
13+
use Magento\AsynchronousOperations\Model\OperationRepositoryInterface;
1314
use Magento\Framework\MessageQueue\MessageValidator;
1415
use Magento\Framework\MessageQueue\MessageEncoder;
1516
use Magento\Framework\Serialize\Serializer\Json;
@@ -18,10 +19,10 @@
1819
/**
1920
* Create operation for list of bulk operations.
2021
*/
21-
class OperationRepository
22+
class OperationRepository implements OperationRepositoryInterface
2223
{
2324
/**
24-
* @var \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory
25+
* @var OperationInterfaceFactory
2526
*/
2627
private $operationFactory;
2728

@@ -67,10 +68,14 @@ public function __construct(
6768
}
6869

6970
/**
70-
* @param $topicName
71-
* @param $entityParams
72-
* @param $groupId
73-
* @return mixed
71+
* Create operation by topic, parameters and group ID
72+
*
73+
* @param string $topicName
74+
* @param array $entityParams
75+
* @param string $groupId
76+
* @return OperationInterface
77+
* @deprecated No longer used.
78+
* @see create()
7479
*/
7580
public function createByTopic($topicName, $entityParams, $groupId)
7681
{
@@ -91,8 +96,16 @@ public function createByTopic($topicName, $entityParams, $groupId)
9196
],
9297
];
9398

94-
/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
99+
/** @var OperationInterface $operation */
95100
$operation = $this->operationFactory->create($data);
96101
return $this->entityManager->save($operation);
97102
}
103+
104+
/**
105+
* @inheritDoc
106+
*/
107+
public function create($topicName, $entityParams, $groupId, $operationId): OperationInterface
108+
{
109+
return $this->createByTopic($topicName, $entityParams, $groupId);
110+
}
98111
}

app/code/Magento/AsynchronousOperations/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<preference for="Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterface" type="Magento\AsynchronousOperations\Model\BulkStatus\Short" />
1919
<preference for="Magento\AsynchronousOperations\Api\Data\OperationSearchResultsInterface" type="Magento\AsynchronousOperations\Model\OperationSearchResults" />
2020
<preference for="Magento\AsynchronousOperations\Api\OperationRepositoryInterface" type="Magento\AsynchronousOperations\Model\OperationRepository" />
21+
<preference for="Magento\AsynchronousOperations\Model\OperationRepositoryInterface" type="Magento\AsynchronousOperations\Model\ResourceModel\Operation\OperationRepository" />
2122
<type name="Magento\Framework\EntityManager\MetadataPool">
2223
<arguments>
2324
<argument name="metadata" xsi:type="array">

app/code/Magento/Webapi/Controller/Rest/InputParamsResolver.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use Magento\Framework\Webapi\ServiceInputProcessor;
1010
use Magento\Framework\Webapi\Rest\Request as RestRequest;
11-
use Magento\Webapi\Controller\Rest\Router;
1211
use Magento\Webapi\Controller\Rest\Router\Route;
1312

1413
/**
@@ -81,7 +80,20 @@ public function resolve()
8180
$route = $this->getRoute();
8281
$serviceMethodName = $route->getServiceMethod();
8382
$serviceClassName = $route->getServiceClass();
83+
$inputData = $this->getInputData();
84+
return $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
85+
}
8486

87+
/**
88+
* Get API input data
89+
*
90+
* @return array
91+
*/
92+
public function getInputData()
93+
{
94+
$route = $this->getRoute();
95+
$serviceMethodName = $route->getServiceMethod();
96+
$serviceClassName = $route->getServiceClass();
8597
/*
8698
* Valid only for updates using PUT when passing id value both in URL and body
8799
*/
@@ -97,9 +109,7 @@ public function resolve()
97109
$inputData = $this->request->getRequestData();
98110
}
99111

100-
$inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
101-
$inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
102-
return $inputParams;
112+
return $this->paramsOverrider->override($inputData, $route->getParameters());
103113
}
104114

105115
/**

app/code/Magento/WebapiAsync/Controller/Rest/Asynchronous/InputParamsResolver.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace Magento\WebapiAsync\Controller\Rest\Asynchronous;
1010

11-
use Magento\Framework\Webapi\ServiceInputProcessor;
1211
use Magento\Framework\Webapi\Rest\Request as RestRequest;
13-
use Magento\Webapi\Controller\Rest\Router;
12+
use Magento\Framework\Webapi\ServiceInputProcessor;
13+
use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver;
1414
use Magento\Webapi\Controller\Rest\ParamsOverrider;
1515
use Magento\Webapi\Controller\Rest\RequestValidator;
16-
use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver;
16+
use Magento\Webapi\Controller\Rest\Router;
1717

1818
/**
1919
* This class is responsible for retrieving resolved input data
@@ -96,19 +96,30 @@ public function resolve()
9696
}
9797
$this->requestValidator->validate();
9898
$webapiResolvedParams = [];
99+
foreach ($this->getInputData() as $key => $singleEntityParams) {
100+
$webapiResolvedParams[$key] = $this->resolveBulkItemParams($singleEntityParams);
101+
}
102+
return $webapiResolvedParams;
103+
}
104+
105+
/**
106+
* Get API input data
107+
*
108+
* @return array
109+
*/
110+
public function getInputData()
111+
{
112+
if ($this->isBulk === false) {
113+
return [$this->inputParamsResolver->getInputData()];
114+
}
99115
$inputData = $this->request->getRequestData();
100116

101117
$httpMethod = $this->request->getHttpMethod();
102118
if ($httpMethod == \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE) {
103119
$requestBodyParams = $this->request->getBodyParams();
104120
$inputData = array_merge($requestBodyParams, $inputData);
105121
}
106-
107-
foreach ($inputData as $key => $singleEntityParams) {
108-
$webapiResolvedParams[$key] = $this->resolveBulkItemParams($singleEntityParams);
109-
}
110-
111-
return $webapiResolvedParams;
122+
return $inputData;
112123
}
113124

114125
/**
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WebapiAsync\Model;
9+
10+
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
11+
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
12+
use Magento\AsynchronousOperations\Model\OperationRepositoryInterface;
13+
use Magento\Framework\MessageQueue\MessageValidator;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\EntityManager\EntityManager;
16+
use Magento\WebapiAsync\Controller\Rest\Asynchronous\InputParamsResolver;
17+
18+
/**
19+
* Repository class to create operation
20+
*/
21+
class OperationRepository implements OperationRepositoryInterface
22+
{
23+
/**
24+
* @var OperationInterfaceFactory
25+
*/
26+
private $operationFactory;
27+
28+
/**
29+
* @var Json
30+
*/
31+
private $jsonSerializer;
32+
33+
/**
34+
* @var EntityManager
35+
*/
36+
private $entityManager;
37+
38+
/**
39+
* @var MessageValidator
40+
*/
41+
private $messageValidator;
42+
/**
43+
* @var InputParamsResolver
44+
*/
45+
private $inputParamsResolver;
46+
47+
/**
48+
* Initialize dependencies.
49+
*
50+
* @param OperationInterfaceFactory $operationFactory
51+
* @param EntityManager $entityManager
52+
* @param MessageValidator $messageValidator
53+
* @param Json $jsonSerializer
54+
* @param InputParamsResolver $inputParamsResolver
55+
*/
56+
public function __construct(
57+
OperationInterfaceFactory $operationFactory,
58+
EntityManager $entityManager,
59+
MessageValidator $messageValidator,
60+
Json $jsonSerializer,
61+
InputParamsResolver $inputParamsResolver
62+
) {
63+
$this->operationFactory = $operationFactory;
64+
$this->jsonSerializer = $jsonSerializer;
65+
$this->messageValidator = $messageValidator;
66+
$this->entityManager = $entityManager;
67+
$this->inputParamsResolver = $inputParamsResolver;
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
public function create($topicName, $entityParams, $groupId, $operationId): OperationInterface
74+
{
75+
$this->messageValidator->validate($topicName, $entityParams);
76+
$requestData = $this->inputParamsResolver->getInputData();
77+
if ($operationId === null || !isset($requestData[$operationId])) {
78+
throw new \InvalidArgumentException(
79+
'Parameter "$operationId" must not be NULL and must exist in input data'
80+
);
81+
}
82+
$encodedMessage = $this->jsonSerializer->serialize($requestData[$operationId]);
83+
84+
$serializedData = [
85+
'entity_id' => null,
86+
'entity_link' => '',
87+
'meta_information' => $encodedMessage,
88+
];
89+
$data = [
90+
'data' => [
91+
OperationInterface::BULK_ID => $groupId,
92+
OperationInterface::TOPIC_NAME => $topicName,
93+
OperationInterface::SERIALIZED_DATA => $this->jsonSerializer->serialize($serializedData),
94+
OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN,
95+
],
96+
];
97+
98+
/** @var OperationInterface $operation */
99+
$operation = $this->operationFactory->create($data);
100+
return $this->entityManager->save($operation);
101+
}
102+
}

app/code/Magento/WebapiAsync/etc/di.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,31 @@
3434
<argument name="isBulk" xsi:type="boolean">true</argument>
3535
</arguments>
3636
</virtualType>
37+
<virtualType name="Magento\WebapiAsync\Model\Bulk\OperationRepository" type="Magento\WebapiAsync\Model\OperationRepository">
38+
<arguments>
39+
<argument name="inputParamsResolver" xsi:type="object">Magento\WebapiAsync\Controller\VirtualType\InputParamsResolver</argument>
40+
</arguments>
41+
</virtualType>
42+
<virtualType name="Magento\WebapiAsync\Model\MassSchedule" type="Magento\AsynchronousOperations\Model\MassSchedule">
43+
<arguments>
44+
<argument name="operationRepository" xsi:type="object">Magento\WebapiAsync\Model\OperationRepository</argument>
45+
</arguments>
46+
</virtualType>
47+
<virtualType name="Magento\WebapiAsync\Model\Bulk\MassSchedule" type="Magento\AsynchronousOperations\Model\MassSchedule">
48+
<arguments>
49+
<argument name="operationRepository" xsi:type="object">Magento\WebapiAsync\Model\Bulk\OperationRepository</argument>
50+
</arguments>
51+
</virtualType>
52+
<type name="Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor">
53+
<arguments>
54+
<argument name="asyncBulkPublisher" xsi:type="object">Magento\WebapiAsync\Model\MassSchedule</argument>
55+
</arguments>
56+
</type>
3757
<virtualType name="Magento\WebapiAsync\Controller\Rest\VirtualType\AsynchronousBulkRequestProcessor" type="Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor">
3858
<arguments>
3959
<argument name="inputParamsResolver" xsi:type="object">Magento\WebapiAsync\Controller\VirtualType\InputParamsResolver</argument>
4060
<argument name="processorPath" xsi:type="const">Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor::BULK_PROCESSOR_PATH</argument>
61+
<argument name="asyncBulkPublisher" xsi:type="object">Magento\WebapiAsync\Model\Bulk\MassSchedule</argument>
4162
</arguments>
4263
</virtualType>
4364
<virtualType name="Magento\WebapiAsync\Controller\Rest\VirtualType\AsynchronousBulkSchemaRequestProcessor" type="Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor">

0 commit comments

Comments
 (0)