Skip to content

Commit aec45cc

Browse files
committed
Change Way of saving Operations to Bulk during sheduling new Bulk Request
1 parent 0d3defe commit aec45cc

File tree

7 files changed

+102
-4
lines changed

7 files changed

+102
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AsynchronousOperations\Api;
10+
11+
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
12+
13+
/**
14+
* Interface for saving multiple operations
15+
*
16+
* @api
17+
*/
18+
interface SaveMultipleOperationsInterface
19+
{
20+
/**
21+
* Save Operations for Bulk
22+
*
23+
* @param OperationInterface[] $operations
24+
* @return void
25+
*/
26+
public function execute(array $operations): void;
27+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\OperationRepository;
2222
use Magento\Authorization\Model\UserContextInterface;
2323
use Magento\Framework\Encryption\Encryptor;
24+
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
2425

2526
/**
2627
* Class MassSchedule used for adding multiple entities as Operations to Bulk Management with the status tracking
@@ -69,6 +70,11 @@ class MassSchedule
6970
*/
7071
private $encryptor;
7172

73+
/**
74+
* @var SaveMultipleOperationsInterface
75+
*/
76+
private $saveMultipleOperations;
77+
7278
/**
7379
* Initialize dependencies.
7480
*
@@ -80,6 +86,7 @@ class MassSchedule
8086
* @param OperationRepository $operationRepository
8187
* @param UserContextInterface $userContext
8288
* @param Encryptor|null $encryptor
89+
* @param SaveMultipleOperationsInterface $saveMultipleOperations
8390
*/
8491
public function __construct(
8592
IdentityGeneratorInterface $identityService,
@@ -89,7 +96,8 @@ public function __construct(
8996
LoggerInterface $logger,
9097
OperationRepository $operationRepository,
9198
UserContextInterface $userContext = null,
92-
Encryptor $encryptor = null
99+
Encryptor $encryptor = null,
100+
SaveMultipleOperationsInterface $saveMultipleOperations
93101
) {
94102
$this->identityService = $identityService;
95103
$this->itemStatusInterfaceFactory = $itemStatusInterfaceFactory;
@@ -99,6 +107,7 @@ public function __construct(
99107
$this->operationRepository = $operationRepository;
100108
$this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
101109
$this->encryptor = $encryptor ?: ObjectManager::getInstance()->get(Encryptor::class);
110+
$this->saveMultipleOperations = $saveMultipleOperations;
102111
}
103112

104113
/**
@@ -133,6 +142,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
133142

134143
$operations = [];
135144
$requestItems = [];
145+
$singleOperationsData = [];
136146
$bulkException = new BulkException();
137147
foreach ($entitiesArray as $key => $entityParams) {
138148
/** @var \Magento\AsynchronousOperations\Api\Data\ItemStatusInterface $requestItem */
@@ -141,6 +151,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
141151
try {
142152
$operation = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
143153
$operations[] = $operation;
154+
$singleOperationsData[] = $operation->getData();
144155
$requestItem->setId($key);
145156
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
146157
$requestItem->setDataHash(
@@ -161,6 +172,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
161172
}
162173
}
163174

175+
$this->saveMultipleOperations->execute($singleOperationsData);
164176
if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
165177
throw new LocalizedException(
166178
__('Something went wrong while processing the request.')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545
$this->operationFactory = $operationFactory;
4646
$this->logger = $logger;
4747
}
48-
48+
4949
/**
5050
* @inheritDoc
5151
*/

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
*/
1212
class Operation extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1313
{
14+
15+
public const TABLE_NAME = "magento_operation";
16+
public const TABLE_PRIMARY_KEY = "id";
17+
1418
/**
1519
* Initialize banner sales rule resource model
1620
*
1721
* @return void
1822
*/
1923
protected function _construct()
2024
{
21-
$this->_init('magento_operation', 'id');
25+
$this->_init(self::TABLE_NAME, self::TABLE_PRIMARY_KEY);
2226
}
2327
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ public function createByTopic($topicName, $entityParams, $groupId)
9393

9494
/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
9595
$operation = $this->operationFactory->create($data);
96-
return $this->entityManager->save($operation);
96+
return $operation;
9797
}
9898
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AsynchronousOperations\Model;
10+
11+
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
12+
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
13+
use Magento\AsynchronousOperations\Model\ResourceModel\Operation as OperationResource;
14+
use Magento\Framework\Exception\CouldNotSaveException;
15+
16+
/**
17+
* Implementation for saving multiple operations
18+
*/
19+
class SaveMultipleOperations implements SaveMultipleOperationsInterface
20+
{
21+
22+
/**
23+
* @var OperationResource
24+
*/
25+
private $operationResource;
26+
27+
/**
28+
* BulkSummary constructor.
29+
*
30+
* @param OperationResource $operationResource
31+
*/
32+
public function __construct(
33+
OperationResource $operationResource
34+
) {
35+
$this->operationResource = $operationResource;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function execute(array $operations): void
42+
{
43+
try {
44+
$connection = $this->operationResource->getConnection();
45+
$connection->insertMultiple(
46+
$this->operationResource->getTable(OperationResource::TABLE_NAME),
47+
$operations
48+
);
49+
} catch (\Exception $exception) {
50+
throw new CouldNotSaveException(__($exception->getMessage()));
51+
}
52+
}
53+
54+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface" type="Magento\AsynchronousOperations\Model\BulkSummary" />
10+
<preference for="Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface" type="Magento\AsynchronousOperations\Model\SaveMultipleOperations" />
1011
<preference for="Magento\AsynchronousOperations\Api\Data\OperationInterface" type="Magento\AsynchronousOperations\Model\Operation" />
1112
<preference for="Magento\AsynchronousOperations\Api\Data\OperationListInterface" type="Magento\AsynchronousOperations\Model\OperationList" />
1213
<preference for="Magento\Framework\Bulk\BulkManagementInterface" type="Magento\AsynchronousOperations\Model\BulkManagement" />

0 commit comments

Comments
 (0)