Advertisement
tuongdn

Consumer.php

Jul 17th, 2020
2,663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. <?php
  2. namespace Magenest\Salesforce\Model\Queue;
  3.  
  4. use Magento\Framework\Exception\LocalizedException;
  5. use Magenest\Salesforce\Model\Queue;
  6. use Magento\Framework\Filesystem;
  7. use Magento\Framework\Notification\NotifierInterface;
  8.  
  9. /**
  10. * Class Consumer
  11. * @package Magenest\Salesforce\Model\Queue
  12. */
  13. class Consumer
  14. {
  15. /* @var \Psr\Log\LoggerInterface /
  16. protected $_logger;
  17.  
  18. /* @var \Magento\Sales\Api\OrderRepositoryInterface /
  19. protected $_orderRepository;
  20.  
  21. /* @var NotifierInterface /
  22. protected $_notifier;
  23.  
  24. /* @var \Magenest\Salesforce\Model\QueueFactory /
  25. protected $_queueFactory;
  26.  
  27. /**
  28. * @var Filesystem
  29. */
  30. private $filesystem;
  31.  
  32. /* @var \Magento\Framework\Serialize\Serializer\Json /
  33. protected $_json;
  34.  
  35. /* @var string /
  36. protected $_type = null;
  37.  
  38. /**
  39. * Consumer constructor.
  40. *
  41. * @param \Psr\Log\LoggerInterface $logger
  42. * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  43. * @param NotifierInterface $notifier
  44. * @param \Magenest\Salesforce\Model\QueueFactory $queueFactory
  45. * @param Filesystem $filesystem
  46. * @param \Magento\Framework\Serialize\Serializer\Json $json
  47. */
  48. public function __construct(
  49. \Psr\Log\LoggerInterface $logger,
  50. \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
  51. NotifierInterface $notifier,
  52. \Magenest\Salesforce\Model\QueueFactory $queueFactory,
  53. Filesystem $filesystem,
  54. \Magento\Framework\Serialize\Serializer\Json $json
  55. ){
  56. $this->_logger = $logger;
  57. $this->_orderRepository = $orderRepository;
  58. $this->_notifier = $notifier;
  59. $this->_queueFactory = $queueFactory;
  60. $this->filesystem = $filesystem;
  61. $this->_json = $json;
  62. }
  63.  
  64. /**
  65. * @param string $orderSearchResult
  66. */
  67. public function process($orderSearchResult)
  68. {
  69. try{
  70. $this->execute($orderSearchResult);
  71. $this->_notifier->addMajor(
  72. __('Your queue are ready'),
  73. __('You can check your orders at Salesforce Queue page')
  74. );
  75. }catch (\Exception $e){
  76. $errorCode = $e->getCode();
  77. $message = __('Sorry, something went wrong during add order to queue. Please see log for details.');
  78. $this->_notifier->addCritical(
  79. $errorCode,
  80. $message
  81. );
  82. $this->_logger->critical($errorCode .": ". $message);
  83. }
  84. }
  85.  
  86. /**
  87. * @param $orderItems
  88. *
  89. * @throws LocalizedException
  90. */
  91. private function execute($orderItems)
  92. {
  93. $orderCollectionArr = [];
  94. /* @var \Magenest\Salesforce\Model\Queue $queue /
  95. $queue = $this->_queueFactory->create();
  96. $orderItems = $this->_json->unserialize($orderItems);
  97. if(is_array($orderItems)){
  98. foreach ($orderItems as $type => $orderId) {
  99. $this->_type = $type;
  100. $orderCollectionArr[] = [
  101. 'type' => $type,
  102. 'entity_id' => $orderId,
  103. 'priority' => 1,
  104. ];
  105. }
  106. $queue->deleteQueueByType($this->_type);
  107. $queue->enqueueMultiRecords($orderCollectionArr);
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement