SubscriptionHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model\Config\Backend\Enabled;
  7. use Magento\Analytics\Model\AnalyticsToken;
  8. use Magento\Analytics\Model\Config\Backend\CollectionTime;
  9. use Magento\Framework\App\Config\ReinitableConfigInterface;
  10. use Magento\Framework\App\Config\Storage\WriterInterface;
  11. use Magento\Framework\FlagManager;
  12. /**
  13. * Class for processing of activation/deactivation MBI subscription.
  14. */
  15. class SubscriptionHandler
  16. {
  17. /**
  18. * Flag code for reserve counter of attempts to subscribe.
  19. */
  20. const ATTEMPTS_REVERSE_COUNTER_FLAG_CODE = 'analytics_link_attempts_reverse_counter';
  21. /**
  22. * Config path for schedule setting of subscription handler.
  23. */
  24. const CRON_STRING_PATH = 'crontab/default/jobs/analytics_subscribe/schedule/cron_expr';
  25. /**
  26. * Config value for schedule setting of subscription handler.
  27. */
  28. const CRON_EXPR_ARRAY = [
  29. '0', # Minute
  30. '*', # Hour
  31. '*', # Day of the Month
  32. '*', # Month of the Year
  33. '*', # Day of the Week
  34. ];
  35. /**
  36. * Max value for reserve counter of attempts to subscribe.
  37. *
  38. * @var int
  39. */
  40. private $attemptsInitValue = 24;
  41. /**
  42. * Service which allows to write values into config.
  43. *
  44. * @var WriterInterface
  45. */
  46. private $configWriter;
  47. /**
  48. * Flag Manager.
  49. *
  50. * @var FlagManager
  51. */
  52. private $flagManager;
  53. /**
  54. * Model for handling Magento BI token value.
  55. *
  56. * @var AnalyticsToken
  57. */
  58. private $analyticsToken;
  59. /**
  60. * @var ReinitableConfigInterface
  61. */
  62. private $reinitableConfig;
  63. /**
  64. * @param WriterInterface $configWriter
  65. * @param FlagManager $flagManager
  66. * @param AnalyticsToken $analyticsToken
  67. * @param ReinitableConfigInterface $reinitableConfig
  68. */
  69. public function __construct(
  70. WriterInterface $configWriter,
  71. FlagManager $flagManager,
  72. AnalyticsToken $analyticsToken,
  73. ReinitableConfigInterface $reinitableConfig
  74. ) {
  75. $this->configWriter = $configWriter;
  76. $this->flagManager = $flagManager;
  77. $this->analyticsToken = $analyticsToken;
  78. $this->reinitableConfig = $reinitableConfig;
  79. }
  80. /**
  81. * Processing of activation MBI subscription.
  82. *
  83. * Activate process of subscription handling if Analytics token is not received.
  84. *
  85. * @return bool
  86. */
  87. public function processEnabled()
  88. {
  89. if (!$this->analyticsToken->isTokenExist()) {
  90. $this->setCronSchedule();
  91. $this->setAttemptsFlag();
  92. $this->reinitableConfig->reinit();
  93. }
  94. return true;
  95. }
  96. /**
  97. * Set cron schedule setting into config for activation of subscription process.
  98. *
  99. * @return bool
  100. */
  101. private function setCronSchedule()
  102. {
  103. $this->configWriter->save(self::CRON_STRING_PATH, join(' ', self::CRON_EXPR_ARRAY));
  104. return true;
  105. }
  106. /**
  107. * Set flag as reserve counter of attempts subscription operation.
  108. *
  109. * @return bool
  110. */
  111. private function setAttemptsFlag()
  112. {
  113. return $this->flagManager
  114. ->saveFlag(self::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue);
  115. }
  116. /**
  117. * Processing of deactivation MBI subscription.
  118. *
  119. * Disable data collection
  120. * and interrupt subscription handling if Analytics token is not received.
  121. *
  122. * @return bool
  123. */
  124. public function processDisabled()
  125. {
  126. $this->disableCollectionData();
  127. if (!$this->analyticsToken->isTokenExist()) {
  128. $this->unsetAttemptsFlag();
  129. }
  130. return true;
  131. }
  132. /**
  133. * Unset flag of attempts subscription operation.
  134. *
  135. * @return bool
  136. */
  137. private function unsetAttemptsFlag()
  138. {
  139. return $this->flagManager
  140. ->deleteFlag(self::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE);
  141. }
  142. /**
  143. * Unset schedule of collection data cron.
  144. *
  145. * @return bool
  146. */
  147. private function disableCollectionData()
  148. {
  149. $this->configWriter->delete(CollectionTime::CRON_SCHEDULE_PATH);
  150. return true;
  151. }
  152. }