AbstractComponent.php 6.11 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Block\Adminhtml\Template;

use Magento\Backend\Block\Widget\Container as WidgetContainer;
use Magento\Backend\Block\Widget\Context as WidgetContext;
use Magento\Backend\Model\Auth\Session;
use Magento\Backend\Model\Auth\StorageInterface;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Integration\Model\Oauth\Token;
use Magento\Security\Model\Config;
use Temando\Shipping\Rest\AuthenticationInterface;
use Temando\Shipping\Webservice\Config\WsConfigInterface;

/**
 * Default block for all pages that display Temando components
 *
 * @package  Temando\Shipping\Block
 * @author   Sebastian Ertner <sebastian.ertner@netresearch.de>
 * @author   Rhodri Davies <rhodri.davies@temando.com>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     http://www.temando.com/
 *
 * @api
 * @deprecated since 1.0.3 | Child blocks will be migrated one after the other.
 * @see \Temando\Shipping\Block\Adminhtml\ComponentContainer
 */
abstract class AbstractComponent extends WidgetContainer
{
    /**
     * @var WsConfigInterface
     */
    private $config;

    /**
     * @var StorageInterface|Session
     */
    private $session;

    /**
     * @var AuthenticationInterface
     */
    private $auth;

    /**
     * @var token
     */
    private $token;

    /**
     * @var DateTime
     */
    private $dateTime;

    /**
     * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
     */
    private $remoteAddress;

    /**
     * @var Config
     */
    private $securityConfig;

    /**
     * AbstractComponent constructor.
     *
     * @param WidgetContext $context
     * @param WsConfigInterface $config
     * @param StorageInterface $session
     * @param AuthenticationInterface $auth
     * @param Token $token
     * @param DateTime $dateTime
     * @param RemoteAddress $remoteAddress
     * @param Config $securityConfig
     * @param array $data
     */
    public function __construct(
        WidgetContext $context,
        WsConfigInterface $config,
        StorageInterface $session,
        AuthenticationInterface $auth,
        Token $token,
        DateTime $dateTime,
        RemoteAddress $remoteAddress,
        Config $securityConfig,
        array $data = []
    ) {
        $this->config         = $config;
        $this->session        = $session;
        $this->auth           = $auth;
        $this->token          = $token;
        $this->dateTime       = $dateTime;
        $this->remoteAddress  = $remoteAddress;
        $this->securityConfig = $securityConfig;

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

    /**
     * Obtain authentication token for Magento REST API access.
     *
     * @return string
     * @throws \Exception
     */
    public function getAccessToken()
    {
        $adminId = $this->session->getUser()->getId();
        $token   = $this->token->loadByAdminId($adminId)->getToken();
        if (!$token) {
            $token = $this->token->createAdminToken($adminId)->getToken();
        } else {
            $this->token->setCreatedAt($this->dateTime->gmtDate());
            $this->token->save();
        }

        return $token;
    }

    /**
     * Obtain Endpoint for Temando REST API access.
     *
     * @return string
     */
    public function getApiEndpoint()
    {
        return $this->config->getApiEndpoint();
    }

    /**
     * Obtain Access Token for Temando REST API access.
     *
     * @return string
     */
    public function getBearerToken()
    {
        return $this->config->getBearerToken();
    }

    /**
     * Obtain Access Token expiry timestamp for Temando REST API access.
     *
     * @return string
     */
    public function getBearerTokenExpiry()
    {
        return $this->config->getBearerTokenExpiry();
    }

    /**
     * Obtain Session Token for Temando REST API access and set it if necessary.
     *
     * @return string
     */
    public function getApiToken()
    {
        $bearerToken = $this->config->getBearerToken();
        $accountId   = $this->config->getAccountId();

        try {
            $this->auth->connect($accountId, $bearerToken);
        } catch (LocalizedException $e) {
            return '';
        }

        return $this->auth->getSessionToken();
    }

    /**
     * Obtain Session Token Expiry for Temando REST API access.
     *
     * @return string
     */
    public function getApiTokenExpiry()
    {
        return $this->auth->getSessionTokenExpiry();
    }

    /**
     * @return string
     */
    public function getLocale()
    {
        $localeCode = $this->_scopeConfig->getValue(
            DirectoryHelper::XML_PATH_DEFAULT_LOCALE,
            ScopeConfigInterface::SCOPE_TYPE_DEFAULT
        );

        return strtolower(str_replace('_', '-', $localeCode));
    }

    /**
     * Obtain Language Code.
     *
     * @return string
     */
    public function getLanguage()
    {
        return substr_replace($this->getLocale(), '', 2);
    }

    /**
     * Obtain componentry assets base url.
     *
     * @return string
     */
    public function getAssetsUrl()
    {
        return $this->getViewFileUrl('Temando_Shipping') . '/';
    }

    /**
     * Obtain Magento HTTP endpoint for session token retrieval.
     *
     * @return string
     */
    public function getApiTokenRefreshEndpoint()
    {
        return $this->_urlBuilder->getUrl('temando/authentication/token');
    }

    /**
     * Obtain merchant IP address.
     *
     * @return string
     */
    public function getIpAddress()
    {
        return $this->remoteAddress->getRemoteAddress();
    }

    /**
     * @return int
     */
    public function getSessionExpirationTime()
    {
        $sessionStart = $this->session->getUpdatedAt();
        $sessionDuration = $this->securityConfig->getAdminSessionLifetime();

        return $sessionStart + $sessionDuration;
    }
}