Feed.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace WeltPixel\Backend\Model;
  3. use Magento\Framework\Config\ConfigOptionsListConstants;
  4. /**
  5. * WeltPixelAdmin Feed model
  6. *
  7. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  8. */
  9. class Feed extends \Magento\Framework\Model\AbstractModel
  10. {
  11. const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
  12. /**
  13. * Feed url
  14. *
  15. * @var string
  16. */
  17. protected $_feedUrl = 'http://weltpixel.com/notifications.rss';
  18. /**
  19. * @var \Magento\Backend\App\ConfigInterface
  20. */
  21. protected $_backendConfig;
  22. /**
  23. * @var \Magento\AdminNotification\Model\InboxFactory
  24. */
  25. protected $_inboxFactory;
  26. /**
  27. * @var \Magento\Framework\HTTP\Adapter\CurlFactory
  28. *
  29. */
  30. protected $curlFactory;
  31. /**
  32. * Deployment configuration
  33. *
  34. * @var \Magento\Framework\App\DeploymentConfig
  35. */
  36. protected $_deploymentConfig;
  37. /**
  38. * @var \Magento\Framework\App\ProductMetadataInterface
  39. */
  40. protected $productMetadata;
  41. /**
  42. * @var \Magento\Framework\UrlInterface
  43. */
  44. protected $urlBuilder;
  45. /**
  46. * @param \Magento\Framework\Model\Context $context
  47. * @param \Magento\Framework\Registry $registry
  48. * @param \Magento\Backend\App\ConfigInterface $backendConfig
  49. * @param \Magento\AdminNotification\Model\InboxFactory $inboxFactory
  50. * @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
  51. * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
  52. * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
  53. * @param \Magento\Framework\UrlInterface $urlBuilder
  54. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  55. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  56. * @param array $data
  57. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  58. */
  59. public function __construct(
  60. \Magento\Framework\Model\Context $context,
  61. \Magento\Framework\Registry $registry,
  62. \Magento\Backend\App\ConfigInterface $backendConfig,
  63. \Magento\AdminNotification\Model\InboxFactory $inboxFactory,
  64. \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
  65. \Magento\Framework\App\DeploymentConfig $deploymentConfig,
  66. \Magento\Framework\App\ProductMetadataInterface $productMetadata,
  67. \Magento\Framework\UrlInterface $urlBuilder,
  68. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  69. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  70. array $data = []
  71. ) {
  72. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  73. $this->_backendConfig = $backendConfig;
  74. $this->_inboxFactory = $inboxFactory;
  75. $this->curlFactory = $curlFactory;
  76. $this->_deploymentConfig = $deploymentConfig;
  77. $this->productMetadata = $productMetadata;
  78. $this->urlBuilder = $urlBuilder;
  79. }
  80. /**
  81. * Init model
  82. *
  83. * @return void
  84. */
  85. protected function _construct()
  86. {
  87. }
  88. /**
  89. * Retrieve feed url
  90. *
  91. * @return string
  92. */
  93. public function getFeedUrl()
  94. {
  95. return $this->_feedUrl;
  96. }
  97. /**
  98. * Check feed for modification
  99. *
  100. * @return $this
  101. */
  102. public function checkUpdate()
  103. {
  104. if ($this->getFrequency() + $this->getLastUpdate() > time()) {
  105. return $this;
  106. }
  107. $feedData = [];
  108. $feedXml = $this->getFeedData();
  109. $installDate = strtotime($this->_deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE));
  110. if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
  111. foreach ($feedXml->channel->item as $item) {
  112. $itemPublicationDate = strtotime((string)$item->pubDate);
  113. if (true || $installDate <= $itemPublicationDate) {
  114. $feedData[] = [
  115. 'severity' => (int)$item->severity,
  116. 'date_added' => date('Y-m-d H:i:s', $itemPublicationDate),
  117. 'title' => (string)$item->title,
  118. 'description' => (string)$item->description,
  119. 'url' => (string)$item->link,
  120. ];
  121. }
  122. }
  123. if ($feedData) {
  124. $this->_inboxFactory->create()->parse(array_reverse($feedData));
  125. }
  126. }
  127. $this->setLastUpdate();
  128. return $this;
  129. }
  130. /**
  131. * Retrieve Update Frequency
  132. *
  133. * @return int
  134. */
  135. public function getFrequency()
  136. {
  137. return $this->_backendConfig->getValue(self::XML_FREQUENCY_PATH) * 3600;
  138. }
  139. /**
  140. * Retrieve Last update time
  141. *
  142. * @return int
  143. */
  144. public function getLastUpdate()
  145. {
  146. return $this->_cacheManager->load('weltpixel_notifications_lastcheck');
  147. }
  148. /**
  149. * Set last update time (now)
  150. *
  151. * @return $this
  152. */
  153. public function setLastUpdate()
  154. {
  155. $this->_cacheManager->save(time(), 'weltpixel_notifications_lastcheck');
  156. return $this;
  157. }
  158. /**
  159. * Retrieve feed data as XML element
  160. *
  161. * @return \SimpleXMLElement
  162. */
  163. public function getFeedData()
  164. {
  165. $curl = $this->curlFactory->create();
  166. $curl->setConfig(
  167. [
  168. 'timeout' => 2,
  169. 'useragent' => $this->productMetadata->getName()
  170. . '/' . $this->productMetadata->getVersion()
  171. . ' (' . $this->productMetadata->getEdition() . ')',
  172. 'referer' => $this->urlBuilder->getUrl('*/*/*')
  173. ]
  174. );
  175. $curl->write(\Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
  176. $data = $curl->read();
  177. if ($data === false) {
  178. return false;
  179. }
  180. $data = preg_split('/^\r?$/m', $data, 2);
  181. $data = trim($data[1]);
  182. $curl->close();
  183. try {
  184. $xml = new \SimpleXMLElement($data);
  185. } catch (\Exception $e) {
  186. return false;
  187. }
  188. return $xml;
  189. }
  190. }