AbstractComponent.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Block\Adminhtml\Template;
  6. use Magento\Backend\Block\Widget\Container as WidgetContainer;
  7. use Magento\Backend\Block\Widget\Context as WidgetContext;
  8. use Magento\Backend\Model\Auth\Session;
  9. use Magento\Backend\Model\Auth\StorageInterface;
  10. use Magento\Directory\Helper\Data as DirectoryHelper;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  14. use Magento\Framework\Stdlib\DateTime\DateTime;
  15. use Magento\Integration\Model\Oauth\Token;
  16. use Magento\Security\Model\Config;
  17. use Temando\Shipping\Rest\AuthenticationInterface;
  18. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  19. /**
  20. * Default block for all pages that display Temando components
  21. *
  22. * @package Temando\Shipping\Block
  23. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  24. * @author Rhodri Davies <rhodri.davies@temando.com>
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. * @link http://www.temando.com/
  27. *
  28. * @api
  29. * @deprecated since 1.0.3 | Child blocks will be migrated one after the other.
  30. * @see \Temando\Shipping\Block\Adminhtml\ComponentContainer
  31. */
  32. abstract class AbstractComponent extends WidgetContainer
  33. {
  34. /**
  35. * @var WsConfigInterface
  36. */
  37. private $config;
  38. /**
  39. * @var StorageInterface|Session
  40. */
  41. private $session;
  42. /**
  43. * @var AuthenticationInterface
  44. */
  45. private $auth;
  46. /**
  47. * @var token
  48. */
  49. private $token;
  50. /**
  51. * @var DateTime
  52. */
  53. private $dateTime;
  54. /**
  55. * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
  56. */
  57. private $remoteAddress;
  58. /**
  59. * @var Config
  60. */
  61. private $securityConfig;
  62. /**
  63. * AbstractComponent constructor.
  64. *
  65. * @param WidgetContext $context
  66. * @param WsConfigInterface $config
  67. * @param StorageInterface $session
  68. * @param AuthenticationInterface $auth
  69. * @param Token $token
  70. * @param DateTime $dateTime
  71. * @param RemoteAddress $remoteAddress
  72. * @param Config $securityConfig
  73. * @param array $data
  74. */
  75. public function __construct(
  76. WidgetContext $context,
  77. WsConfigInterface $config,
  78. StorageInterface $session,
  79. AuthenticationInterface $auth,
  80. Token $token,
  81. DateTime $dateTime,
  82. RemoteAddress $remoteAddress,
  83. Config $securityConfig,
  84. array $data = []
  85. ) {
  86. $this->config = $config;
  87. $this->session = $session;
  88. $this->auth = $auth;
  89. $this->token = $token;
  90. $this->dateTime = $dateTime;
  91. $this->remoteAddress = $remoteAddress;
  92. $this->securityConfig = $securityConfig;
  93. parent::__construct($context, $data);
  94. }
  95. /**
  96. * Obtain authentication token for Magento REST API access.
  97. *
  98. * @return string
  99. * @throws \Exception
  100. */
  101. public function getAccessToken()
  102. {
  103. $adminId = $this->session->getUser()->getId();
  104. $token = $this->token->loadByAdminId($adminId)->getToken();
  105. if (!$token) {
  106. $token = $this->token->createAdminToken($adminId)->getToken();
  107. } else {
  108. $this->token->setCreatedAt($this->dateTime->gmtDate());
  109. $this->token->save();
  110. }
  111. return $token;
  112. }
  113. /**
  114. * Obtain Endpoint for Temando REST API access.
  115. *
  116. * @return string
  117. */
  118. public function getApiEndpoint()
  119. {
  120. return $this->config->getApiEndpoint();
  121. }
  122. /**
  123. * Obtain Access Token for Temando REST API access.
  124. *
  125. * @return string
  126. */
  127. public function getBearerToken()
  128. {
  129. return $this->config->getBearerToken();
  130. }
  131. /**
  132. * Obtain Access Token expiry timestamp for Temando REST API access.
  133. *
  134. * @return string
  135. */
  136. public function getBearerTokenExpiry()
  137. {
  138. return $this->config->getBearerTokenExpiry();
  139. }
  140. /**
  141. * Obtain Session Token for Temando REST API access and set it if necessary.
  142. *
  143. * @return string
  144. */
  145. public function getApiToken()
  146. {
  147. $bearerToken = $this->config->getBearerToken();
  148. $accountId = $this->config->getAccountId();
  149. try {
  150. $this->auth->connect($accountId, $bearerToken);
  151. } catch (LocalizedException $e) {
  152. return '';
  153. }
  154. return $this->auth->getSessionToken();
  155. }
  156. /**
  157. * Obtain Session Token Expiry for Temando REST API access.
  158. *
  159. * @return string
  160. */
  161. public function getApiTokenExpiry()
  162. {
  163. return $this->auth->getSessionTokenExpiry();
  164. }
  165. /**
  166. * @return string
  167. */
  168. public function getLocale()
  169. {
  170. $localeCode = $this->_scopeConfig->getValue(
  171. DirectoryHelper::XML_PATH_DEFAULT_LOCALE,
  172. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  173. );
  174. return strtolower(str_replace('_', '-', $localeCode));
  175. }
  176. /**
  177. * Obtain Language Code.
  178. *
  179. * @return string
  180. */
  181. public function getLanguage()
  182. {
  183. return substr_replace($this->getLocale(), '', 2);
  184. }
  185. /**
  186. * Obtain componentry assets base url.
  187. *
  188. * @return string
  189. */
  190. public function getAssetsUrl()
  191. {
  192. return $this->getViewFileUrl('Temando_Shipping') . '/';
  193. }
  194. /**
  195. * Obtain Magento HTTP endpoint for session token retrieval.
  196. *
  197. * @return string
  198. */
  199. public function getApiTokenRefreshEndpoint()
  200. {
  201. return $this->_urlBuilder->getUrl('temando/authentication/token');
  202. }
  203. /**
  204. * Obtain merchant IP address.
  205. *
  206. * @return string
  207. */
  208. public function getIpAddress()
  209. {
  210. return $this->remoteAddress->getRemoteAddress();
  211. }
  212. /**
  213. * @return int
  214. */
  215. public function getSessionExpirationTime()
  216. {
  217. $sessionStart = $this->session->getUpdatedAt();
  218. $sessionDuration = $this->securityConfig->getAdminSessionLifetime();
  219. return $sessionStart + $sessionDuration;
  220. }
  221. }