ReportUrlProvider.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
  8. use Magento\Analytics\Model\Connector\OTPRequest;
  9. use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\FlagManager;
  12. /**
  13. * Provide URL on resource with reports.
  14. */
  15. class ReportUrlProvider
  16. {
  17. /**
  18. * Resource for handling MBI token value.
  19. *
  20. * @var AnalyticsToken
  21. */
  22. private $analyticsToken;
  23. /**
  24. * Resource which provide OTP.
  25. *
  26. * @var OTPRequest
  27. */
  28. private $otpRequest;
  29. /**
  30. * @var ScopeConfigInterface
  31. */
  32. private $config;
  33. /**
  34. * @var FlagManager
  35. */
  36. private $flagManager;
  37. /**
  38. * Path to config value with URL which provide reports.
  39. *
  40. * @var string
  41. */
  42. private $urlReportConfigPath = 'analytics/url/report';
  43. /**
  44. * @param AnalyticsToken $analyticsToken
  45. * @param OTPRequest $otpRequest
  46. * @param ScopeConfigInterface $config
  47. * @param FlagManager $flagManager
  48. */
  49. public function __construct(
  50. AnalyticsToken $analyticsToken,
  51. OTPRequest $otpRequest,
  52. ScopeConfigInterface $config,
  53. FlagManager $flagManager
  54. ) {
  55. $this->analyticsToken = $analyticsToken;
  56. $this->otpRequest = $otpRequest;
  57. $this->config = $config;
  58. $this->flagManager = $flagManager;
  59. }
  60. /**
  61. * Provide URL on resource with reports.
  62. *
  63. * @return string
  64. * @throws SubscriptionUpdateException
  65. */
  66. public function getUrl()
  67. {
  68. if ($this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE)) {
  69. throw new SubscriptionUpdateException(__(
  70. 'Your Base URL has been changed and your reports are being updated. '
  71. . 'Advanced Reporting will be available once this change has been processed. Please try again later.'
  72. ));
  73. }
  74. $url = $this->config->getValue($this->urlReportConfigPath);
  75. if ($this->analyticsToken->isTokenExist()) {
  76. $otp = $this->otpRequest->call();
  77. if ($otp) {
  78. $query = http_build_query(['otp' => $otp], '', '&');
  79. $url .= '?' . $query;
  80. }
  81. }
  82. return $url;
  83. }
  84. }