1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File:OrderExportHandler.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* Github: https://github.com/maciejslawik |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\GenericOrderExport\Model; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use Magento\Framework\MessageQueue\PublisherInterface; |
15
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
16
|
|
|
use MSlwk\GenericOrderExport\Api\Amqp\ExportOrderMessageInterface; |
17
|
|
|
use MSlwk\GenericOrderExport\Api\Amqp\ExportOrderMessageInterfaceFactory; |
18
|
|
|
use MSlwk\GenericOrderExport\Api\ConfigProviderInterface; |
19
|
|
|
use MSlwk\GenericOrderExport\Api\OrderExportHandlerInterface; |
20
|
|
|
use MSlwk\GenericOrderExport\Api\OrderExportServiceInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class OrderExportHandler |
25
|
|
|
* @package MSlwk\GenericOrderExport\Model |
26
|
|
|
*/ |
27
|
|
|
class OrderExportHandler implements OrderExportHandlerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private const TOPIC_ORDER_EXPORT = 'exportNewOrdersTopic'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ConfigProviderInterface |
36
|
|
|
*/ |
37
|
|
|
private $configProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ExportOrderMessageInterfaceFactory |
41
|
|
|
*/ |
42
|
|
|
private $exportOrderMessageFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var PublisherInterface |
46
|
|
|
*/ |
47
|
|
|
private $publisher; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var OrderExportServiceInterface |
51
|
|
|
*/ |
52
|
|
|
private $orderExportService; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var LoggerInterface |
56
|
|
|
*/ |
57
|
|
|
private $logger; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* ExportOrderAfterPlace constructor. |
61
|
|
|
* @param ConfigProviderInterface $configProvider |
62
|
|
|
* @param ExportOrderMessageInterfaceFactory $exportOrderMessageFactory |
63
|
|
|
* @param PublisherInterface $publisher |
64
|
|
|
* @param OrderExportServiceInterface $orderExportService |
65
|
|
|
* @param LoggerInterface $logger |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
ConfigProviderInterface $configProvider, |
69
|
|
|
ExportOrderMessageInterfaceFactory $exportOrderMessageFactory, |
70
|
|
|
PublisherInterface $publisher, |
71
|
|
|
OrderExportServiceInterface $orderExportService, |
72
|
|
|
LoggerInterface $logger |
73
|
|
|
) { |
74
|
|
|
$this->configProvider = $configProvider; |
75
|
|
|
$this->exportOrderMessageFactory = $exportOrderMessageFactory; |
76
|
|
|
$this->publisher = $publisher; |
77
|
|
|
$this->orderExportService = $orderExportService; |
78
|
|
|
$this->logger = $logger; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param OrderInterface $order |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function handleOrderExport(OrderInterface $order): void |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
if ($this->configProvider->isExportAsync()) { |
89
|
|
|
/** @var ExportOrderMessageInterface $message */ |
90
|
|
|
$message = $this->exportOrderMessageFactory->create(); |
91
|
|
|
$message->setOrderId((string)$order->getEntityId()); |
92
|
|
|
$this->publisher->publish(self::TOPIC_ORDER_EXPORT, $message); |
93
|
|
|
} else { |
94
|
|
|
$this->orderExportService->exportOrder($order); |
95
|
|
|
} |
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
$this->logger->error($e->getMessage(), ['order_id' => $order->getEntityId()]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|