OrderStatusHistoryUpdater.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Order\AutoProcessing;
  6. use Magento\Framework\Exception\CouldNotSaveException;
  7. use Magento\Sales\Api\Data\OrderInterface;
  8. use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
  9. use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory;
  10. use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
  11. use Temando\Shipping\Model\Shipment\ShipmentErrorInterface;
  12. /**
  13. * Temando Order Fulfillment Comments History Updater.
  14. *
  15. * @package Temando\Shipping\Model
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link http://www.temando.com/
  19. */
  20. class OrderStatusHistoryUpdater
  21. {
  22. /**
  23. * @var OrderStatusHistoryInterfaceFactory
  24. */
  25. private $historyFactory;
  26. /**
  27. * @var OrderStatusHistoryRepositoryInterface
  28. */
  29. private $historyRepository;
  30. /**
  31. * OrderStatusHistoryUpdater constructor.
  32. * @param OrderStatusHistoryInterfaceFactory $historyFactory
  33. * @param OrderStatusHistoryRepositoryInterface $historyRepository
  34. */
  35. public function __construct(
  36. OrderStatusHistoryInterfaceFactory $historyFactory,
  37. OrderStatusHistoryRepositoryInterface $historyRepository
  38. ) {
  39. $this->historyFactory = $historyFactory;
  40. $this->historyRepository = $historyRepository;
  41. }
  42. /**
  43. * @param OrderInterface $order
  44. * @param string $comment
  45. * @return bool
  46. */
  47. public function addComment(OrderInterface $order, $comment)
  48. {
  49. $historyItem = $this->historyFactory->create(['data' => [
  50. OrderStatusHistoryInterface::COMMENT => $comment,
  51. OrderStatusHistoryInterface::PARENT_ID => $order->getEntityId(),
  52. OrderStatusHistoryInterface::ENTITY_NAME => 'order',
  53. OrderStatusHistoryInterface::STATUS => $order->getStatus(),
  54. OrderStatusHistoryInterface::IS_CUSTOMER_NOTIFIED => false,
  55. ]]);
  56. try {
  57. $this->historyRepository->save($historyItem);
  58. return true;
  59. } catch (CouldNotSaveException $exception) {
  60. return false;
  61. }
  62. }
  63. /**
  64. * @param OrderInterface $order
  65. * @param ShipmentErrorInterface[] $errors
  66. * @return void
  67. */
  68. public function addErrors(OrderInterface $order, array $errors)
  69. {
  70. foreach ($errors as $error) {
  71. $comment = $error->getDetail()
  72. ? sprintf('%s - %s', $error->getTitle(), $error->getDetail())
  73. : $error->getTitle();
  74. $this->addComment($order, $comment);
  75. }
  76. }
  77. }