CaseInfo.php 2.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Signifyd\Block\Adminhtml;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Signifyd\Api\Data\CaseInterface;
use Magento\Signifyd\Model\CaseManagement;

/**
 * Get Signifyd Case Info
 *
 * @api
 * @since 100.2.0
 */
class CaseInfo extends Template
{
    /**
     * @var CaseInterface
     */
    private $caseEntity = false;

    /**
     * @var CaseManagement
     */
    private $caseManagement;

    /**
     * @param Context $context
     * @param CaseManagement $caseManagement
     * @param array $data
     */
    public function __construct(
        Context $context,
        CaseManagement $caseManagement,
        array $data = []
    ) {
        $this->caseManagement = $caseManagement;

        parent::__construct($context, $data);
    }

    /**
     * Gets case entity associated with order id.
     *
     * @return CaseInterface|null
     */
    private function getCaseEntity()
    {
        if ($this->caseEntity === false) {
            $this->caseEntity = $this->caseManagement->getByOrderId(
                $this->getOrderId()
            );
        }

        return $this->caseEntity;
    }

    /**
     * Default getter for case properties
     *
     * @param mixed $defaultValue
     * @param callable $callback
     * @return mixed
     */
    private function getCaseProperty($defaultValue, callable $callback)
    {
        return $this->isEmptyCase() ? $defaultValue : call_user_func($callback);
    }

    /**
     * Checks if case is exists for order
     *
     * @return bool
     * @since 100.2.0
     */
    public function isEmptyCase()
    {
        return $this->getCaseEntity() === null;
    }

    /**
     * Gets case guarantee disposition status.
     *
     * @return string
     * @since 100.2.0
     */
    public function getCaseGuaranteeDisposition()
    {
        return $this->getCaseProperty('', function () {
            $guaranteeStatusMap = [
                CaseInterface::GUARANTEE_APPROVED => __('Approved'),
                CaseInterface::GUARANTEE_DECLINED => __('Declined'),
                CaseInterface::GUARANTEE_PENDING => __('Pending'),
                CaseInterface::GUARANTEE_CANCELED => __('Canceled'),
                CaseInterface::GUARANTEE_IN_REVIEW => __('In Review'),
                CaseInterface::GUARANTEE_UNREQUESTED => __('Unrequested')
            ];

            $status = isset($guaranteeStatusMap[$this->getCaseEntity()->getGuaranteeDisposition()]) ?
                $guaranteeStatusMap[$this->getCaseEntity()->getGuaranteeDisposition()] :
                '';

            return $status;
        });
    }

    /**
     * Retrieves current order Id.
     *
     * @return integer
     */
    private function getOrderId()
    {
        return (int) $this->getRequest()->getParam('order_id');
    }
}