CreateGuaranteeAbility.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\Guarantee;
  7. use Magento\Framework\Exception\InputException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Intl\DateTimeFactory;
  10. use Magento\Sales\Api\Data\OrderInterface;
  11. use Magento\Sales\Api\OrderRepositoryInterface;
  12. use Magento\Sales\Model\Order;
  13. use Magento\Signifyd\Model\CaseManagement;
  14. /**
  15. * Checks if is possible to create Guarantee for order.
  16. */
  17. class CreateGuaranteeAbility
  18. {
  19. /**
  20. * @var CaseManagement
  21. */
  22. private $caseManagement;
  23. /**
  24. * @var OrderRepositoryInterface
  25. */
  26. private $orderRepository;
  27. /**
  28. * @var DateTimeFactory
  29. */
  30. private $dateTimeFactory;
  31. /**
  32. * Eligible count of days from the order creation date to submit a case for Guarantee.
  33. *
  34. * @var int
  35. */
  36. private static $guarantyEligibleDays = 7;
  37. /**
  38. * @param CaseManagement $caseManagement
  39. * @param OrderRepositoryInterface $orderRepository
  40. * @param DateTimeFactory $dateTimeFactory
  41. */
  42. public function __construct(
  43. CaseManagement $caseManagement,
  44. OrderRepositoryInterface $orderRepository,
  45. DateTimeFactory $dateTimeFactory
  46. ) {
  47. $this->caseManagement = $caseManagement;
  48. $this->orderRepository = $orderRepository;
  49. $this->dateTimeFactory = $dateTimeFactory;
  50. }
  51. /**
  52. * Checks if it is possible to create Guarantee for order and case.
  53. *
  54. * @param int $orderId
  55. * @return bool
  56. */
  57. public function isAvailable($orderId)
  58. {
  59. $case = $this->caseManagement->getByOrderId($orderId);
  60. if (null === $case) {
  61. return false;
  62. }
  63. if ($case->isGuaranteeEligible() === false) {
  64. return false;
  65. }
  66. $order = $this->getOrder($orderId);
  67. if (null === $order) {
  68. return false;
  69. }
  70. if (in_array($order->getState(), [Order::STATE_CANCELED, Order::STATE_CLOSED])) {
  71. return false;
  72. }
  73. if ($this->isOrderOlderThen(static::$guarantyEligibleDays, $order)) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * Checks if Guarantee submit is applicable for order.
  80. *
  81. * @param OrderInterface $order
  82. * @param int $days number of days from the order creation date to submit a case for Guarantee.
  83. * @return bool
  84. */
  85. private function isOrderOlderThen($days, OrderInterface $order)
  86. {
  87. $orderCreateDate = $this->dateTimeFactory->create($order->getCreatedAt(), new \DateTimeZone('UTC'));
  88. $currentDate = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
  89. return $orderCreateDate->diff($currentDate)->days >= $days;
  90. }
  91. /**
  92. * Returns order by id
  93. *
  94. * @param int $orderId
  95. * @return OrderInterface|null
  96. */
  97. private function getOrder($orderId)
  98. {
  99. try {
  100. $order = $this->orderRepository->get($orderId);
  101. } catch (InputException $e) {
  102. return null;
  103. } catch (NoSuchEntityException $e) {
  104. return null;
  105. }
  106. return $order;
  107. }
  108. }