From 38297af11f458b30af3cbdc42cf3407d6aff44ab Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 2 Sep 2013 02:43:25 +0200 Subject: move configuration values into a config file, fix #167 --- utility/config.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 utility/config.php (limited to 'utility') diff --git a/utility/config.php b/utility/config.php new file mode 100644 index 000000000..e00ad940f --- /dev/null +++ b/utility/config.php @@ -0,0 +1,147 @@ +. +* +*/ + +namespace OCA\News\Utility; + +use \OCA\AppFramework\Core\API; + + +class Config { + + private $fileSystem; + private $autoPurgeMinimumInterval; // seconds, used to define how + // long deleted folders and feeds + // should still be kept for an + // undo actions + private $autoPurgeCount; // number of allowed unread articles per feed + private $simplePieCacheDuration; // seconds + private $feedFetcherTimeout; // seconds + private $useCronUpdates; // turn off updates run by owncloud cronjob + private $api; + + + public function __construct($fileSystem, API $api) { + $this->fileSystem = $fileSystem; + $this->autoPurgeMinimumInterval = 60; + $this->autoPurgeCount = 200; + $this->simplePieCacheDuration = 30*60; + $this->feedFetcherTimeout = 60; + $this->useCronUpdates = true; + $this->api = $api; + } + + + public function getAutoPurgeMinimumInterval() { + return $this->autoPurgeMinimumInterval; + } + + + public function getAutoPurgeCount() { + return $this->autoPurgeCount; + } + + + public function getSimplePieCacheDuration() { + return $this->simplePieCacheDuration; + } + + + public function getFeedFetcherTimeout() { + return $this->feedFetcherTimeout; + } + + + public function getUseCronUpdates() { + return $this->useCronUpdates; + } + + + public function setAutoPurgeMinimumInterval($value) { + $this->autoPurgeMinimumInterval = $value; + } + + + public function setAutoPurgeCount($value) { + $this->autoPurgeCount = $value; + } + + + public function setSimplePieCacheDuration($value) { + $this->simplePieCacheDuration = $value; + } + + + public function setFeedFetcherTimeout($value) { + $this->feedFetcherTimeout = $value; + } + + + public function setUseCronUpdates($value) { + $this->useCronUpdates = $value; + } + + + public function read($configPath, $createIfNotExists=false) { + if($createIfNotExists && !$this->fileSystem->file_exists($configPath)) { + + $this->write($configPath); + + } else { + + $content = $this->fileSystem->file_get_contents($configPath); + $configValues = json_decode($content, true); + + if($configValues === null) { + $this->api->log('Configuration contains invalid JSON' , 'warn'); + } else { + + foreach($configValues as $key => $value) { + if(property_exists($this, $key)) { + $this->$key = $value; + } else { + $this->api->log('Configuration value "' . $key . + '" does not exist. Ignored value.' , 'warn'); + } + } + + } + } + } + + + public function write($configPath) { + $json = json_encode(array( + "autoPurgeMinimumInterval" => $this->autoPurgeMinimumInterval, + "autoPurgeCount" => $this->autoPurgeCount, + "simplePieCacheDuration" => $this->simplePieCacheDuration, + "feedFetcherTimeout" => $this->feedFetcherTimeout, + "useCronUpdates" => $this->useCronUpdates, + ), JSON_PRETTY_PRINT); + + $this->fileSystem->file_put_contents($configPath, $json); + } + + +} \ No newline at end of file -- cgit v1.2.3