AdminNotificationFeed.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model;
  9. use Magento\Framework\Config\ConfigOptionsListConstants;
  10. /**
  11. * Blog admin notification feed model
  12. */
  13. class AdminNotificationFeed extends \Magento\AdminNotification\Model\Feed
  14. {
  15. /**
  16. * @var \Magento\Backend\Model\Auth\Session
  17. */
  18. protected $_backendAuthSession;
  19. /**
  20. * @var \Magento\Framework\Module\ModuleListInterface
  21. */
  22. protected $_moduleList;
  23. /**
  24. * @var \Magento\Framework\Module\Manager
  25. */
  26. protected $_moduleManager;
  27. /**
  28. * @param \Magento\Framework\Model\Context $context
  29. * @param \Magento\Framework\Registry $registry
  30. * @param \Magento\Backend\App\ConfigInterface $backendConfig
  31. * @param InboxFactory $inboxFactory
  32. * @param \Magento\Backend\Model\Auth\Session $backendAuthSession
  33. * @param \Magento\Framework\Module\ModuleListInterface $moduleList
  34. * @param \Magento\Framework\Module\Manager $moduleManager,
  35. * @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
  36. * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
  37. * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
  38. * @param \Magento\Framework\UrlInterface $urlBuilder
  39. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  40. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  41. * @param array $data
  42. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  43. */
  44. public function __construct(
  45. \Magento\Framework\Model\Context $context,
  46. \Magento\Framework\Registry $registry,
  47. \Magento\Backend\App\ConfigInterface $backendConfig,
  48. \Magento\AdminNotification\Model\InboxFactory $inboxFactory,
  49. \Magento\Backend\Model\Auth\Session $backendAuthSession,
  50. \Magento\Framework\Module\ModuleListInterface $moduleList,
  51. \Magento\Framework\Module\Manager $moduleManager,
  52. \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
  53. \Magento\Framework\App\DeploymentConfig $deploymentConfig,
  54. \Magento\Framework\App\ProductMetadataInterface $productMetadata,
  55. \Magento\Framework\UrlInterface $urlBuilder,
  56. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  57. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  58. array $data = []
  59. ) {
  60. parent::__construct($context, $registry, $backendConfig, $inboxFactory, $curlFactory, $deploymentConfig, $productMetadata, $urlBuilder, $resource, $resourceCollection, $data);
  61. $this->_backendAuthSession = $backendAuthSession;
  62. $this->_moduleList = $moduleList;
  63. $this->_moduleManager = $moduleManager;
  64. }
  65. /**
  66. * Retrieve feed url
  67. *
  68. * @return string
  69. */
  70. public function getFeedUrl()
  71. {
  72. if (is_null($this->_feedUrl)) {
  73. $this->_feedUrl = 'http://mage'.'fan'
  74. .'.c'.'om/community/notifications'.'/'.'feed/';
  75. }
  76. $urlInfo = parse_url($this->urlBuilder->getBaseUrl());
  77. $domain = isset($urlInfo['host']) ? $urlInfo['host'] : '';
  78. $url = $this->_feedUrl . 'domain/' . urlencode($domain);
  79. $modulesParams = [];
  80. foreach($this->getMagefanModules() as $key => $module) {
  81. $key = str_replace('Magefan_', '', $key);
  82. $modulesParams[] = $key . ',' . $module['setup_version'];
  83. }
  84. if (count($modulesParams)) {
  85. $url .= '/modules/'.base64_encode(implode(';', $modulesParams));
  86. }
  87. return $url;
  88. }
  89. /**
  90. * Get Magefan Modules Info
  91. *
  92. * @return $this
  93. */
  94. protected function getMagefanModules()
  95. {
  96. $modules = [];
  97. foreach($this->_moduleList->getAll() as $moduleName => $module) {
  98. if ( strpos($moduleName, 'Magefan_') !== false && $this->_moduleManager->isEnabled($moduleName) ) {
  99. $modules[$moduleName] = $module;
  100. }
  101. }
  102. return $modules;
  103. }
  104. /**
  105. * Check feed for modification
  106. *
  107. * @return $this
  108. */
  109. public function checkUpdate()
  110. {
  111. $session = $this->_backendAuthSession;
  112. $time = time();
  113. $frequency = $this->getFrequency();
  114. if (($frequency + $session->getMfNoticeLastUpdate() > $time)
  115. || ($frequency + $this->getLastUpdate() > $time)
  116. ) {
  117. return $this;
  118. }
  119. $session->setMfNoticeLastUpdate($time);
  120. return parent::checkUpdate();
  121. }
  122. /**
  123. * Retrieve Update Frequency
  124. *
  125. * @return int
  126. */
  127. public function getFrequency()
  128. {
  129. return 86400;
  130. }
  131. /**
  132. * Retrieve Last update time
  133. *
  134. * @return int
  135. */
  136. public function getLastUpdate()
  137. {
  138. return $this->_cacheManager->load('magefan_admin_notifications_lastcheck');
  139. }
  140. /**
  141. * Set last update time (now)
  142. *
  143. * @return $this
  144. */
  145. public function setLastUpdate()
  146. {
  147. $this->_cacheManager->save(time(), 'magefan_admin_notifications_lastcheck');
  148. return $this;
  149. }
  150. }