ShippingApiAccess.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\DataProvider;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Framework\UrlInterface;
  8. use Temando\Shipping\Rest\AuthenticationInterface;
  9. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  10. /**
  11. * Temando API Access Provider
  12. *
  13. * @package Temando\Shipping\ViewModel
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link http://www.temando.com/
  17. */
  18. class ShippingApiAccess implements ShippingApiAccessInterface
  19. {
  20. /**
  21. * @var AuthenticationInterface
  22. */
  23. private $auth;
  24. /**
  25. * @var WsConfigInterface
  26. */
  27. private $config;
  28. /**
  29. * @var UrlInterface
  30. */
  31. private $urlBuilder;
  32. /**
  33. * ApiAccess constructor.
  34. * @param AuthenticationInterface $auth
  35. * @param WsConfigInterface $config
  36. * @param UrlInterface $urlBuilder
  37. */
  38. public function __construct(
  39. AuthenticationInterface $auth,
  40. WsConfigInterface $config,
  41. UrlInterface $urlBuilder
  42. ) {
  43. $this->auth = $auth;
  44. $this->config = $config;
  45. $this->urlBuilder = $urlBuilder;
  46. }
  47. /**
  48. * Obtain Endpoint for Temando REST API access.
  49. *
  50. * @return string
  51. */
  52. public function getApiEndpoint(): string
  53. {
  54. return (string) $this->config->getApiEndpoint();
  55. }
  56. /**
  57. * Obtain Session Token for Temando REST API access and set it if necessary.
  58. *
  59. * @return string
  60. */
  61. public function getSessionToken(): string
  62. {
  63. $bearerToken = $this->config->getBearerToken();
  64. $accountId = $this->config->getAccountId();
  65. try {
  66. $this->auth->connect($accountId, $bearerToken);
  67. } catch (LocalizedException $e) {
  68. return '';
  69. }
  70. return (string) $this->auth->getSessionToken();
  71. }
  72. /**
  73. * Obtain Session Token Expiry for Temando REST API access.
  74. *
  75. * @return string
  76. */
  77. public function getSessionTokenExpiry(): string
  78. {
  79. return (string) $this->auth->getSessionTokenExpiry();
  80. }
  81. /**
  82. * Obtain Session Token Retrieval Endpoint
  83. *
  84. * @return string
  85. */
  86. public function getSessionTokenRefreshEndpoint(): string
  87. {
  88. return (string) $this->urlBuilder->getUrl('temando/authentication/token');
  89. }
  90. }