CommentsHistoryUpdater.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model;
  7. use Magento\Framework\Phrase;
  8. use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
  9. use Magento\Sales\Model\Order\Status\HistoryFactory;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. /**
  12. * Updates case order comments history.
  13. */
  14. class CommentsHistoryUpdater
  15. {
  16. /**
  17. * @var HistoryFactory
  18. */
  19. private $historyFactory;
  20. /**
  21. * @var OrderStatusHistoryRepositoryInterface
  22. */
  23. private $historyRepository;
  24. /**
  25. * CommentsHistoryUpdater constructor.
  26. *
  27. * @param HistoryFactory $historyFactory
  28. * @param OrderStatusHistoryRepositoryInterface $historyRepository
  29. */
  30. public function __construct(
  31. HistoryFactory $historyFactory,
  32. OrderStatusHistoryRepositoryInterface $historyRepository
  33. ) {
  34. $this->historyFactory = $historyFactory;
  35. $this->historyRepository = $historyRepository;
  36. }
  37. /**
  38. * Adds comment to case related order.
  39. * Throws an exception if cannot save history comment.
  40. *
  41. * @param CaseInterface $case
  42. * @param Phrase $message
  43. * @param string $status
  44. * @return void
  45. */
  46. public function addComment(CaseInterface $case, Phrase $message, $status = '')
  47. {
  48. if (!$message->getText()) {
  49. return;
  50. }
  51. /** @var \Magento\Sales\Api\Data\OrderStatusHistoryInterface $history */
  52. $history = $this->historyFactory->create();
  53. $history->setParentId($case->getOrderId())
  54. ->setComment($message)
  55. ->setEntityName('order')
  56. ->setStatus($status);
  57. $this->historyRepository->save($history);
  58. }
  59. }