PortalUrl.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config;
  6. use Magento\Framework\App\ProductMetadata;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Module\PackageInfo;
  9. use Temando\Shipping\Rest\AuthenticationInterface;
  10. /**
  11. * Portal URL provider
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Nathan Wilson <nathan.wilson@temando.com>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class PortalUrl
  19. {
  20. /**
  21. * @var AuthenticationInterface
  22. */
  23. private $auth;
  24. /**
  25. * @var ModuleConfig
  26. */
  27. private $config;
  28. /**
  29. * @var ProductMetadata
  30. */
  31. private $metadata;
  32. /**
  33. * @var PackageInfo
  34. */
  35. private $packageInfo;
  36. /**
  37. * PortalUrl constructor.
  38. * @param AuthenticationInterface $auth
  39. * @param ModuleConfig $config
  40. * @param ProductMetadata $metadata
  41. * @param PackageInfo $packageInfo
  42. */
  43. public function __construct(
  44. AuthenticationInterface $auth,
  45. ModuleConfig $config,
  46. ProductMetadata $metadata,
  47. PackageInfo $packageInfo
  48. ) {
  49. $this->auth = $auth;
  50. $this->config = $config;
  51. $this->metadata = $metadata;
  52. $this->packageInfo = $packageInfo;
  53. }
  54. /**
  55. * Collect platform and account details.
  56. *
  57. * @return string[]
  58. * @throws LocalizedException
  59. */
  60. private function getQueryParams(): array
  61. {
  62. $bearerToken = $this->config->getBearerToken();
  63. $accountId = $this->config->getAccountId();
  64. $this->auth->connect($accountId, $bearerToken);
  65. $queryParams = [
  66. 'platform' => [
  67. 'name' => $this->metadata->getName(),
  68. 'version' => $this->metadata->getVersion(),
  69. 'extensionName' => 'Temando_Shipping',
  70. 'extensionVersion' => $this->packageInfo->getVersion('Temando_Shipping'),
  71. ],
  72. '[session]accountId' => $accountId,
  73. '[session]apiUrl' => $this->config->getApiEndpoint(),
  74. '[session]token' => $this->auth->getSessionToken(),
  75. '[session]expiry' => $this->auth->getSessionTokenExpiry()
  76. ];
  77. return $queryParams;
  78. }
  79. /**
  80. * Obtain the Shipping Portal URL, account section.
  81. *
  82. * @return string
  83. * @throws LocalizedException
  84. */
  85. public function getAccountUrl(): string
  86. {
  87. $portalUrl = $this->config->getShippingPortalUrl();
  88. $queryParams = $this->getQueryParams();
  89. $portalUrl = sprintf('%s%s?%s', $portalUrl, 'account', http_build_query($queryParams));
  90. return $portalUrl;
  91. }
  92. /**
  93. * Obtain the Shipping Portal URL, shipping experiences section.
  94. *
  95. * @return string
  96. * @throws LocalizedException
  97. */
  98. public function getExperiencesUrl(): string
  99. {
  100. $portalUrl = $this->config->getShippingPortalUrl();
  101. $queryParams = $this->getQueryParams();
  102. $portalUrl = sprintf('%s%s?%s', $portalUrl, 'shipping-experiences', http_build_query($queryParams));
  103. return $portalUrl;
  104. }
  105. }