_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; } }