123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace WeltPixel\Backend\Model;
- use Magento\Framework\Config\ConfigOptionsListConstants;
- /**
- * WeltPixelAdmin Feed model
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Feed extends \Magento\Framework\Model\AbstractModel
- {
- const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
- /**
- * Feed url
- *
- * @var string
- */
- protected $_feedUrl = 'http://weltpixel.com/notifications.rss';
- /**
- * @var \Magento\Backend\App\ConfigInterface
- */
- protected $_backendConfig;
- /**
- * @var \Magento\AdminNotification\Model\InboxFactory
- */
- protected $_inboxFactory;
- /**
- * @var \Magento\Framework\HTTP\Adapter\CurlFactory
- *
- */
- protected $curlFactory;
- /**
- * Deployment configuration
- *
- * @var \Magento\Framework\App\DeploymentConfig
- */
- protected $_deploymentConfig;
- /**
- * @var \Magento\Framework\App\ProductMetadataInterface
- */
- protected $productMetadata;
- /**
- * @var \Magento\Framework\UrlInterface
- */
- protected $urlBuilder;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Backend\App\ConfigInterface $backendConfig
- * @param \Magento\AdminNotification\Model\InboxFactory $inboxFactory
- * @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
- * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
- * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
- * @param \Magento\Framework\UrlInterface $urlBuilder
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Backend\App\ConfigInterface $backendConfig,
- \Magento\AdminNotification\Model\InboxFactory $inboxFactory,
- \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
- \Magento\Framework\App\DeploymentConfig $deploymentConfig,
- \Magento\Framework\App\ProductMetadataInterface $productMetadata,
- \Magento\Framework\UrlInterface $urlBuilder,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- parent::__construct($context, $registry, $resource, $resourceCollection, $data);
- $this->_backendConfig = $backendConfig;
- $this->_inboxFactory = $inboxFactory;
- $this->curlFactory = $curlFactory;
- $this->_deploymentConfig = $deploymentConfig;
- $this->productMetadata = $productMetadata;
- $this->urlBuilder = $urlBuilder;
- }
- /**
- * Init model
- *
- * @return void
- */
- protected function _construct()
- {
- }
- /**
- * Retrieve feed url
- *
- * @return string
- */
- public function getFeedUrl()
- {
- return $this->_feedUrl;
- }
- /**
- * Check feed for modification
- *
- * @return $this
- */
- public function checkUpdate()
- {
- if ($this->getFrequency() + $this->getLastUpdate() > time()) {
- return $this;
- }
- $feedData = [];
- $feedXml = $this->getFeedData();
- $installDate = strtotime($this->_deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE));
- if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
- foreach ($feedXml->channel->item as $item) {
- $itemPublicationDate = strtotime((string)$item->pubDate);
- if (true || $installDate <= $itemPublicationDate) {
- $feedData[] = [
- 'severity' => (int)$item->severity,
- 'date_added' => date('Y-m-d H:i:s', $itemPublicationDate),
- 'title' => (string)$item->title,
- 'description' => (string)$item->description,
- 'url' => (string)$item->link,
- ];
- }
- }
- if ($feedData) {
- $this->_inboxFactory->create()->parse(array_reverse($feedData));
- }
- }
- $this->setLastUpdate();
- return $this;
- }
- /**
- * Retrieve Update Frequency
- *
- * @return int
- */
- public function getFrequency()
- {
- return $this->_backendConfig->getValue(self::XML_FREQUENCY_PATH) * 3600;
- }
- /**
- * Retrieve Last update time
- *
- * @return int
- */
- public function getLastUpdate()
- {
- return $this->_cacheManager->load('weltpixel_notifications_lastcheck');
- }
- /**
- * Set last update time (now)
- *
- * @return $this
- */
- public function setLastUpdate()
- {
- $this->_cacheManager->save(time(), 'weltpixel_notifications_lastcheck');
- return $this;
- }
- /**
- * Retrieve feed data as XML element
- *
- * @return \SimpleXMLElement
- */
- public function getFeedData()
- {
- $curl = $this->curlFactory->create();
- $curl->setConfig(
- [
- 'timeout' => 2,
- 'useragent' => $this->productMetadata->getName()
- . '/' . $this->productMetadata->getVersion()
- . ' (' . $this->productMetadata->getEdition() . ')',
- 'referer' => $this->urlBuilder->getUrl('*/*/*')
- ]
- );
- $curl->write(\Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
- $data = $curl->read();
- if ($data === false) {
- return false;
- }
- $data = preg_split('/^\r?$/m', $data, 2);
- $data = trim($data[1]);
- $curl->close();
- try {
- $xml = new \SimpleXMLElement($data);
- } catch (\Exception $e) {
- return false;
- }
- return $xml;
- }
- }
|