Skip to content

Commit 7758019

Browse files
committed
magento-engcom/bulk-api#4 Support for Async operations in WebAPI
- Fixed static tests
1 parent 35928c2 commit 7758019

File tree

5 files changed

+62
-42
lines changed

5 files changed

+62
-42
lines changed

app/code/Magento/AsynchronousOperations/Api/BulkStatusInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\AsynchronousOperations\Api;
810

911
/**

app/code/Magento/AsynchronousOperations/Api/Data/BulkStatusInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/**
1212
* Interface BulkStatusInterface
1313
*
14+
* Bulk summary data with list of operations items summary data.
15+
*
1416
* @api
1517
*/
1618
interface BulkStatusInterface extends BulkSummaryInterface

app/code/Magento/AsynchronousOperations/Api/Data/DetailedBulkStatusInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* Interface BulkStatusInterface
13+
*
1314
* Bulk summary data with list of operations items full data.
1415
*
1516
* @api

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
/**
2020
* Class BulkStatus
21-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2221
*/
2322
class BulkOperationsStatus implements BulkStatusInterface
2423
{

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

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535

3636
/**
3737
* Class Consumer used to process OperationInterface messages.
38-
* This could be used for both synchronous and asynchronous processing, depending on topic.
39-
*
40-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4138
*/
4239
class MassConsumer implements ConsumerInterface
4340
{
@@ -119,7 +116,6 @@ public function __construct(
119116
$this->jsonHelper = $jsonHelper;
120117
$this->operationManagement = $operationManagement;
121118
$this->messageController = $messageController;
122-
123119
$this->logger = $logger ? : \Magento\Framework\App\ObjectManager::getInstance()->get(LoggerInterface::class);
124120
}
125121

@@ -188,7 +184,6 @@ private function getTransactionCallback(QueueInterface $queue)
188184
*
189185
* @param EnvelopeInterface $message
190186
* @throws LocalizedException
191-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
192187
*/
193188
private function dispatchMessage(EnvelopeInterface $message)
194189
{
@@ -213,42 +208,10 @@ private function dispatchMessage(EnvelopeInterface $message)
213208

214209
if ($errorCode === null) {
215210
foreach ($handlers as $callback) {
216-
try {
217-
call_user_func_array($callback, $entityParams);
218-
$messages[] = sprintf('Service execution success %s::%s', get_class($callback[0]), $callback[1]);
219-
} catch (\Zend_Db_Adapter_Exception $e) {
220-
$this->logger->critical($e->getMessage());
221-
if ($e instanceof LockWaitException
222-
|| $e instanceof DeadlockException
223-
|| $e instanceof ConnectionException
224-
) {
225-
$status = OperationInterface::STATUS_TYPE_RETRIABLY_FAILED;
226-
$errorCode = $e->getCode();
227-
$messages[] = __($e->getMessage());
228-
} else {
229-
$status = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
230-
$errorCode = $e->getCode();
231-
$messages[] =
232-
__('Sorry, something went wrong during product prices update. Please see log for details.');
233-
}
234-
} catch (NoSuchEntityException $e) {
235-
$this->logger->error($e->getMessage());
236-
$status = ($e instanceof TemporaryStateExceptionInterface) ?
237-
OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED :
238-
OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
239-
$errorCode = $e->getCode();
240-
$messages[] = $e->getMessage();
241-
} catch (LocalizedException $e) {
242-
$this->logger->error($e->getMessage());
243-
$status = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
244-
$errorCode = $e->getCode();
245-
$messages[] = $e->getMessage();
246-
} catch (\Exception $e) {
247-
$this->logger->error($e->getMessage());
248-
$status = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
249-
$errorCode = $e->getCode();
250-
$messages[] = $e->getMessage();
251-
}
211+
$result = $this->executeHandler($callback, $entityParams);
212+
$status = $result['status'];
213+
$errorCode = $result['error_code'];
214+
$messages = array_merge($messages, $result['messages']);
252215
}
253216
}
254217

@@ -261,4 +224,57 @@ private function dispatchMessage(EnvelopeInterface $message)
261224
$serializedData
262225
);
263226
}
227+
228+
/**
229+
* Execute topic handler
230+
*
231+
* @param $callback
232+
* @param $entityParams
233+
* @return array
234+
*/
235+
private function executeHandler($callback, $entityParams)
236+
{
237+
$result = [
238+
'status' => OperationInterface::STATUS_TYPE_COMPLETE,
239+
'error_code' => null,
240+
'messages' => []
241+
];
242+
try {
243+
call_user_func_array($callback, $entityParams);
244+
$messages[] = sprintf('Service execution success %s::%s', get_class($callback[0]), $callback[1]);
245+
} catch (\Zend_Db_Adapter_Exception $e) {
246+
$this->logger->critical($e->getMessage());
247+
if ($e instanceof LockWaitException
248+
|| $e instanceof DeadlockException
249+
|| $e instanceof ConnectionException
250+
) {
251+
$result['status'] = OperationInterface::STATUS_TYPE_RETRIABLY_FAILED;
252+
$result['error_code'] = $e->getCode();
253+
$result['messages'][] = __($e->getMessage());
254+
} else {
255+
$result['status'] = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
256+
$result['error_code'] = $e->getCode();
257+
$result['messages'][] =
258+
__('Sorry, something went wrong during product prices update. Please see log for details.');
259+
}
260+
} catch (NoSuchEntityException $e) {
261+
$this->logger->error($e->getMessage());
262+
$result['status'] = ($e instanceof TemporaryStateExceptionInterface) ?
263+
OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED :
264+
OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
265+
$result['error_code'] = $e->getCode();
266+
$result['messages'][] = $e->getMessage();
267+
} catch (LocalizedException $e) {
268+
$this->logger->error($e->getMessage());
269+
$result['status'] = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
270+
$result['error_code'] = $e->getCode();
271+
$result['messages'][] = $e->getMessage();
272+
} catch (\Exception $e) {
273+
$this->logger->error($e->getMessage());
274+
$result['status'] = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
275+
$result['error_code'] = $e->getCode();
276+
$result['messages'][] = $e->getMessage();
277+
}
278+
return $result;
279+
}
264280
}

0 commit comments

Comments
 (0)