SubscriptionStatusProvider.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Config\Backend\Enabled\SubscriptionHandler;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\FlagManager;
  11. /**
  12. * Provider of subscription status.
  13. */
  14. class SubscriptionStatusProvider
  15. {
  16. /**
  17. * Represents an enabled subscription state.
  18. */
  19. const ENABLED = "Enabled";
  20. /**
  21. * Represents a failed subscription state.
  22. */
  23. const FAILED = "Failed";
  24. /**
  25. * Represents a pending subscription state.
  26. */
  27. const PENDING = "Pending";
  28. /**
  29. * Represents a disabled subscription state.
  30. */
  31. const DISABLED = "Disabled";
  32. /**
  33. * @var ScopeConfigInterface
  34. */
  35. private $scopeConfig;
  36. /**
  37. * @var AnalyticsToken
  38. */
  39. private $analyticsToken;
  40. /**
  41. * @var FlagManager
  42. */
  43. private $flagManager;
  44. /**
  45. * @param ScopeConfigInterface $scopeConfig
  46. * @param AnalyticsToken $analyticsToken
  47. * @param FlagManager $flagManager
  48. */
  49. public function __construct(
  50. ScopeConfigInterface $scopeConfig,
  51. AnalyticsToken $analyticsToken,
  52. FlagManager $flagManager
  53. ) {
  54. $this->scopeConfig = $scopeConfig;
  55. $this->analyticsToken = $analyticsToken;
  56. $this->flagManager = $flagManager;
  57. }
  58. /**
  59. * Retrieve subscription status to Magento BI Advanced Reporting.
  60. *
  61. * Statuses:
  62. * Enabled - if subscription is enabled and MA token was received;
  63. * Pending - if subscription is enabled and MA token was not received;
  64. * Disabled - if subscription is not enabled.
  65. * Failed - if subscription is enabled and token was not received after attempts ended.
  66. *
  67. * @return string
  68. */
  69. public function getStatus()
  70. {
  71. $isSubscriptionEnabledInConfig = $this->scopeConfig->getValue('analytics/subscription/enabled');
  72. if ($isSubscriptionEnabledInConfig) {
  73. return $this->getStatusForEnabledSubscription();
  74. }
  75. return $this->getStatusForDisabledSubscription();
  76. }
  77. /**
  78. * Retrieve status for subscription that enabled in config.
  79. *
  80. * @return string
  81. */
  82. public function getStatusForEnabledSubscription()
  83. {
  84. $status = static::ENABLED;
  85. if ($this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE)) {
  86. $status = self::PENDING;
  87. }
  88. if (!$this->analyticsToken->isTokenExist()) {
  89. $status = static::PENDING;
  90. if ($this->flagManager->getFlagData(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE) === null) {
  91. $status = static::FAILED;
  92. }
  93. }
  94. return $status;
  95. }
  96. /**
  97. * Retrieve status for subscription that disabled in config.
  98. *
  99. * @return string
  100. */
  101. public function getStatusForDisabledSubscription()
  102. {
  103. return static::DISABLED;
  104. }
  105. }