Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Feed.php
Go to the documentation of this file.
1 <?php
7 
9 
19 {
20  const XML_USE_HTTPS_PATH = 'system/adminnotification/use_https';
21 
22  const XML_FEED_URL_PATH = 'system/adminnotification/feed_url';
23 
24  const XML_FREQUENCY_PATH = 'system/adminnotification/frequency';
25 
26  const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';
27 
33  protected $_feedUrl;
34 
38  protected $_backendConfig;
39 
43  protected $_inboxFactory;
44 
49  protected $curlFactory;
50 
56  protected $_deploymentConfig;
57 
61  protected $productMetadata;
62 
66  protected $urlBuilder;
67 
82  public function __construct(
83  \Magento\Framework\Model\Context $context,
84  \Magento\Framework\Registry $registry,
85  \Magento\Backend\App\ConfigInterface $backendConfig,
86  \Magento\AdminNotification\Model\InboxFactory $inboxFactory,
87  \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
88  \Magento\Framework\App\DeploymentConfig $deploymentConfig,
89  \Magento\Framework\App\ProductMetadataInterface $productMetadata,
90  \Magento\Framework\UrlInterface $urlBuilder,
91  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
92  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
93  array $data = []
94  ) {
95  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
96  $this->_backendConfig = $backendConfig;
97  $this->_inboxFactory = $inboxFactory;
98  $this->curlFactory = $curlFactory;
99  $this->_deploymentConfig = $deploymentConfig;
100  $this->productMetadata = $productMetadata;
101  $this->urlBuilder = $urlBuilder;
102  }
103 
109  protected function _construct()
110  {
111  }
112 
118  public function getFeedUrl()
119  {
120  $httpPath = $this->_backendConfig->isSetFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://';
121  if ($this->_feedUrl === null) {
122  $this->_feedUrl = $httpPath . $this->_backendConfig->getValue(self::XML_FEED_URL_PATH);
123  }
124  return $this->_feedUrl;
125  }
126 
132  public function checkUpdate()
133  {
134  if ($this->getFrequency() + $this->getLastUpdate() > time()) {
135  return $this;
136  }
137 
138  $feedData = [];
139 
140  $feedXml = $this->getFeedData();
141 
142  $installDate = strtotime($this->_deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE));
143 
144  if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
145  foreach ($feedXml->channel->item as $item) {
146  $itemPublicationDate = strtotime((string)$item->pubDate);
147  if ($installDate <= $itemPublicationDate) {
148  $feedData[] = [
149  'severity' => (int)$item->severity,
150  'date_added' => date('Y-m-d H:i:s', $itemPublicationDate),
151  'title' => $this->escapeString($item->title),
152  'description' => $this->escapeString($item->description),
153  'url' => $this->escapeString($item->link),
154  ];
155  }
156  }
157 
158  if ($feedData) {
159  $this->_inboxFactory->create()->parse(array_reverse($feedData));
160  }
161  }
162  $this->setLastUpdate();
163 
164  return $this;
165  }
166 
172  public function getFrequency()
173  {
174  return $this->_backendConfig->getValue(self::XML_FREQUENCY_PATH) * 3600;
175  }
176 
182  public function getLastUpdate()
183  {
184  return $this->_cacheManager->load('admin_notifications_lastcheck');
185  }
186 
192  public function setLastUpdate()
193  {
194  $this->_cacheManager->save(time(), 'admin_notifications_lastcheck');
195  return $this;
196  }
197 
203  public function getFeedData()
204  {
205  $curl = $this->curlFactory->create();
206  $curl->setConfig(
207  [
208  'timeout' => 2,
209  'useragent' => $this->productMetadata->getName()
210  . '/' . $this->productMetadata->getVersion()
211  . ' (' . $this->productMetadata->getEdition() . ')',
212  'referer' => $this->urlBuilder->getUrl('*/*/*')
213  ]
214  );
215  $curl->write(\Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
216  $data = $curl->read();
217  $data = preg_split('/^\r?$/m', $data, 2);
218  $data = trim($data[1]);
219  $curl->close();
220 
221  try {
222  $xml = new \SimpleXMLElement($data);
223  } catch (\Exception $e) {
224  return false;
225  }
226 
227  return $xml;
228  }
229 
235  public function getFeedXml()
236  {
237  try {
238  $data = $this->getFeedData();
239  $xml = new \SimpleXMLElement($data);
240  } catch (\Exception $e) {
241  $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
242  }
243 
244  return $xml;
245  }
246 
253  private function escapeString(\SimpleXMLElement $data)
254  {
255  return htmlspecialchars((string)$data);
256  }
257 }
$resource
Definition: bulk.php:12
$deploymentConfig
__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=[])
Definition: Feed.php:82