diff options
author | emkael <emkael@tlen.pl> | 2017-01-16 17:32:19 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-01-16 17:32:47 +0100 |
commit | 68f5ac77ee0ca7788c7c48681ad2067de2af427b (patch) | |
tree | a9d99d54578c117787e76da2ab00b9b99bfc44ee /providers/Provider.php | |
parent | 4ea787e34ddbfa62781fa6ea4b6fd4af39ef9ff0 (diff) |
* new structure, for multiple providers
Diffstat (limited to 'providers/Provider.php')
-rw-r--r-- | providers/Provider.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/providers/Provider.php b/providers/Provider.php new file mode 100644 index 0000000..0c3f344 --- /dev/null +++ b/providers/Provider.php @@ -0,0 +1,54 @@ +<?php + +namespace Providers; + +abstract class Provider { + + protected $_options = []; + protected $_feed = NULL; + protected $_cacheTimeout = '15 minutes'; + protected $_cacheTime; + + public function __construct($feed, $options=[]) { + $this->_feed = $feed; + $this->_options = $options; + } + + abstract protected function _getCachePath(); + + protected function _getCache($path) { + return file_get_contents($path); + } + + abstract protected function _fetchItems(); + + abstract protected function _spamFilter($items); + + protected function _getItems() { + $cacheFile = sprintf($this->_getCachePath(), $this->_feed); + $this->_cacheTime = file_exists($cacheFile) ? filemtime($cacheFile) : 0; + if ($this->_cacheTime > strtotime('-' . $this->_cacheTimeout)) { + return json_decode($this->_getCache($cacheFile)); + } else { + $content = $this->_fetchItems(); + file_put_contents($cacheFile, json_encode($content)); + $this->_cacheTime = time(); + return $content; + } + } + + public function get() { + $items = $this->_getItems(); + if (isset($this->_options['spamfilter'])) { + $items = $this->_spamFilter($items); + } + return $items; + } + + public function cacheTime() { + return $this->_cacheTime; + } + +} + +?> |