UpdatingService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\CaseServices;
  7. use Magento\Framework\Api\SimpleDataObjectConverter;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Signifyd\Api\CaseRepositoryInterface;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. use Magento\Signifyd\Model\CommentsHistoryUpdater;
  12. use Magento\Signifyd\Model\MessageGenerators\GeneratorInterface;
  13. use Magento\Signifyd\Model\OrderStateService;
  14. use Magento\Signifyd\Model\SalesOrderGrid\OrderGridUpdater;
  15. /**
  16. * Performs Signifyd case entity updating operations.
  17. */
  18. class UpdatingService implements UpdatingServiceInterface
  19. {
  20. /**
  21. * @var GeneratorInterface
  22. */
  23. private $messageGenerator;
  24. /**
  25. * @var CaseRepositoryInterface
  26. */
  27. private $caseRepository;
  28. /**
  29. * @var CommentsHistoryUpdater
  30. */
  31. private $commentsHistoryUpdater;
  32. /**
  33. * @var \Magento\Signifyd\Model\SalesOrderGrid\OrderGridUpdater
  34. */
  35. private $orderGridUpdater;
  36. /**
  37. * @var OrderStateService
  38. */
  39. private $orderStateService;
  40. /**
  41. * UpdatingService constructor.
  42. *
  43. * @param GeneratorInterface $messageGenerator
  44. * @param CaseRepositoryInterface $caseRepository
  45. * @param CommentsHistoryUpdater $commentsHistoryUpdater
  46. * @param \Magento\Signifyd\Model\SalesOrderGrid\OrderGridUpdater $orderGridUpdater
  47. * @param OrderStateService $orderStateService
  48. */
  49. public function __construct(
  50. GeneratorInterface $messageGenerator,
  51. CaseRepositoryInterface $caseRepository,
  52. CommentsHistoryUpdater $commentsHistoryUpdater,
  53. OrderGridUpdater $orderGridUpdater,
  54. OrderStateService $orderStateService
  55. ) {
  56. $this->messageGenerator = $messageGenerator;
  57. $this->caseRepository = $caseRepository;
  58. $this->commentsHistoryUpdater = $commentsHistoryUpdater;
  59. $this->orderGridUpdater = $orderGridUpdater;
  60. $this->orderStateService = $orderStateService;
  61. }
  62. /**
  63. * Updates Signifyd Case entity by received data.
  64. *
  65. * @param CaseInterface $case
  66. * @param array $data
  67. * @return void
  68. * @throws LocalizedException
  69. */
  70. public function update(CaseInterface $case, array $data)
  71. {
  72. if (empty($case->getEntityId()) || empty($case->getCaseId())) {
  73. throw new LocalizedException(__('The case entity should not be empty.'));
  74. }
  75. try {
  76. $previousDisposition = $case->getGuaranteeDisposition();
  77. $this->setCaseData($case, $data);
  78. $orderHistoryComment = $this->messageGenerator->generate($data);
  79. $case = $this->caseRepository->save($case);
  80. $this->orderGridUpdater->update($case->getOrderId());
  81. $this->commentsHistoryUpdater->addComment($case, $orderHistoryComment);
  82. if ($case->getGuaranteeDisposition() !== $previousDisposition) {
  83. $this->orderStateService->updateByCase($case);
  84. }
  85. } catch (\Exception $e) {
  86. throw new LocalizedException(__('Cannot update Case entity.'), $e);
  87. }
  88. }
  89. /**
  90. * Sets data to case entity.
  91. *
  92. * @param CaseInterface $case
  93. * @param array $data
  94. * @return void
  95. */
  96. private function setCaseData(CaseInterface $case, array $data)
  97. {
  98. // list of keys which should not be replaced, like order id
  99. $notResolvedKeys = [
  100. 'orderId'
  101. ];
  102. foreach ($data as $key => $value) {
  103. $methodName = 'set' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key);
  104. if (!in_array($key, $notResolvedKeys) && method_exists($case, $methodName)) {
  105. call_user_func([$case, $methodName], $value);
  106. }
  107. }
  108. }
  109. }