CaseInfo.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Block\Adminhtml;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Framework\View\Element\Template\Context;
  9. use Magento\Signifyd\Api\Data\CaseInterface;
  10. use Magento\Signifyd\Model\CaseManagement;
  11. /**
  12. * Get Signifyd Case Info
  13. *
  14. * @api
  15. * @since 100.2.0
  16. */
  17. class CaseInfo extends Template
  18. {
  19. /**
  20. * @var CaseInterface
  21. */
  22. private $caseEntity = false;
  23. /**
  24. * @var CaseManagement
  25. */
  26. private $caseManagement;
  27. /**
  28. * @param Context $context
  29. * @param CaseManagement $caseManagement
  30. * @param array $data
  31. */
  32. public function __construct(
  33. Context $context,
  34. CaseManagement $caseManagement,
  35. array $data = []
  36. ) {
  37. $this->caseManagement = $caseManagement;
  38. parent::__construct($context, $data);
  39. }
  40. /**
  41. * Gets case entity associated with order id.
  42. *
  43. * @return CaseInterface|null
  44. */
  45. private function getCaseEntity()
  46. {
  47. if ($this->caseEntity === false) {
  48. $this->caseEntity = $this->caseManagement->getByOrderId(
  49. $this->getOrderId()
  50. );
  51. }
  52. return $this->caseEntity;
  53. }
  54. /**
  55. * Default getter for case properties
  56. *
  57. * @param mixed $defaultValue
  58. * @param callable $callback
  59. * @return mixed
  60. */
  61. private function getCaseProperty($defaultValue, callable $callback)
  62. {
  63. return $this->isEmptyCase() ? $defaultValue : call_user_func($callback);
  64. }
  65. /**
  66. * Checks if case is exists for order
  67. *
  68. * @return bool
  69. * @since 100.2.0
  70. */
  71. public function isEmptyCase()
  72. {
  73. return $this->getCaseEntity() === null;
  74. }
  75. /**
  76. * Gets case guarantee disposition status.
  77. *
  78. * @return string
  79. * @since 100.2.0
  80. */
  81. public function getCaseGuaranteeDisposition()
  82. {
  83. return $this->getCaseProperty('', function () {
  84. $guaranteeStatusMap = [
  85. CaseInterface::GUARANTEE_APPROVED => __('Approved'),
  86. CaseInterface::GUARANTEE_DECLINED => __('Declined'),
  87. CaseInterface::GUARANTEE_PENDING => __('Pending'),
  88. CaseInterface::GUARANTEE_CANCELED => __('Canceled'),
  89. CaseInterface::GUARANTEE_IN_REVIEW => __('In Review'),
  90. CaseInterface::GUARANTEE_UNREQUESTED => __('Unrequested')
  91. ];
  92. $status = isset($guaranteeStatusMap[$this->getCaseEntity()->getGuaranteeDisposition()]) ?
  93. $guaranteeStatusMap[$this->getCaseEntity()->getGuaranteeDisposition()] :
  94. '';
  95. return $status;
  96. });
  97. }
  98. /**
  99. * Retrieves current order Id.
  100. *
  101. * @return integer
  102. */
  103. private function getOrderId()
  104. {
  105. return (int) $this->getRequest()->getParam('order_id');
  106. }
  107. }