summaryrefslogtreecommitdiff
path: root/providers/Provider.php
diff options
context:
space:
mode:
Diffstat (limited to 'providers/Provider.php')
-rw-r--r--providers/Provider.php54
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;
+ }
+
+}
+
+?>