CancelGuaranteeAbility.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Sales\Api\Data\OrderInterface;
  10. use Magento\Sales\Api\OrderRepositoryInterface;
  11. use Magento\Signifyd\Model\CaseManagement;
  12. /**
  13. * Checks if is possible to cancel Guarantee for order.
  14. */
  15. class CancelGuaranteeAbility
  16. {
  17. /**
  18. * @var CaseManagement
  19. */
  20. private $caseManagement;
  21. /**
  22. * @var OrderRepositoryInterface
  23. */
  24. private $orderRepository;
  25. /**
  26. * @param CaseManagement $caseManagement
  27. * @param OrderRepositoryInterface $orderRepository
  28. */
  29. public function __construct(
  30. CaseManagement $caseManagement,
  31. OrderRepositoryInterface $orderRepository
  32. ) {
  33. $this->caseManagement = $caseManagement;
  34. $this->orderRepository = $orderRepository;
  35. }
  36. /**
  37. * Checks if it is possible to create Guarantee for order and case.
  38. *
  39. * @param int $orderId
  40. * @return bool
  41. */
  42. public function isAvailable($orderId)
  43. {
  44. $case = $this->caseManagement->getByOrderId($orderId);
  45. if ($case === null) {
  46. return false;
  47. }
  48. if (in_array($case->getGuaranteeDisposition(), [null, $case::GUARANTEE_CANCELED])) {
  49. return false;
  50. }
  51. $order = $this->getOrder($orderId);
  52. if (null === $order) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Returns order by id
  59. *
  60. * @param int $orderId
  61. * @return OrderInterface|null
  62. */
  63. private function getOrder($orderId)
  64. {
  65. try {
  66. $order = $this->orderRepository->get($orderId);
  67. } catch (InputException $e) {
  68. return null;
  69. } catch (NoSuchEntityException $e) {
  70. return null;
  71. }
  72. return $order;
  73. }
  74. }