Config   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 162
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getAutoPurgeMinimumInterval() 0 7 2
A getAutoPurgeCount() 0 3 1
A getMaxRedirects() 0 3 1
A getFeedFetcherTimeout() 0 3 1
A getUseCronUpdates() 0 3 1
A getMaxSize() 0 3 1
A getExploreUrl() 0 3 1
A setAutoPurgeMinimumInterval() 0 3 1
A setAutoPurgeCount() 0 3 1
A setMaxRedirects() 0 3 1
A setFeedFetcherTimeout() 0 3 1
A setUseCronUpdates() 0 3 1
A setMaxSize() 0 3 1
A setExploreUrl() 0 3 1
C read() 0 34 7
A write() 0 20 1
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Config;
15
16
use OCP\ILogger;
17
use OCP\Files\Folder;
18
19
20
class Config {
21
22
    private $fileSystem;
23
    private $autoPurgeMinimumInterval;  // seconds, used to define how
24
                                        // long deleted folders and feeds
25
                                        // should still be kept for an
26
                                        // undo actions
27
    private $autoPurgeCount;  // number of allowed unread articles per feed
28
    private $maxRedirects;  // seconds
29
    private $feedFetcherTimeout;  // seconds
30
    private $useCronUpdates;  // turn off updates run by owncloud cronjob
31
    private $logger;
32
    private $loggerParams;
33
    private $maxSize;
34
    private $exploreUrl;
35
36
    public function __construct(Folder $fileSystem,
37
                                ILogger $logger,
38
                                $LoggerParameters) {
39
        $this->fileSystem = $fileSystem;
40
        $this->autoPurgeMinimumInterval = 60;
41
        $this->autoPurgeCount = 200;
42
        $this->maxRedirects = 10;
43
        $this->maxSize = 100*1024*1024; // 100Mb
44
        $this->feedFetcherTimeout = 60;
45
        $this->useCronUpdates = true;
46
        $this->logger = $logger;
47
        $this->exploreUrl = '';
48
        $this->loggerParams = $LoggerParameters;
49
    }
50
51
    public function getAutoPurgeMinimumInterval() {
52
        if ($this->autoPurgeMinimumInterval > 60) {
53
            return $this->autoPurgeMinimumInterval;
54
        } else {
55
            return 60;
56
        }
57
    }
58
59
    public function getAutoPurgeCount() {
60
        return $this->autoPurgeCount;
61
    }
62
63
64
    public function getMaxRedirects() {
65
        return $this->maxRedirects;
66
    }
67
68
69
    public function getFeedFetcherTimeout() {
70
        return $this->feedFetcherTimeout;
71
    }
72
73
74
    public function getUseCronUpdates() {
75
        return $this->useCronUpdates;
76
    }
77
78
79
    public function getMaxSize() {
80
        return $this->maxSize;
81
    }
82
83
84
    public function getExploreUrl() {
85
        return $this->exploreUrl;
86
    }
87
88
89
    public function setAutoPurgeMinimumInterval($value) {
90
        $this->autoPurgeMinimumInterval = $value;
91
    }
92
93
94
    public function setAutoPurgeCount($value) {
95
        $this->autoPurgeCount = $value;
96
    }
97
98
99
    public function setMaxRedirects($value) {
100
        $this->maxRedirects = $value;
101
    }
102
103
104
    public function setFeedFetcherTimeout($value) {
105
        $this->feedFetcherTimeout = $value;
106
    }
107
108
109
    public function setUseCronUpdates($value) {
110
        $this->useCronUpdates = $value;
111
    }
112
113
    public function setMaxSize($value) {
114
        $this->maxSize = $value;
115
    }
116
117
118
    public function setExploreUrl($value) {
119
        $this->exploreUrl = $value;
120
    }
121
122
123
    public function read($configPath, $createIfNotExists=false) {
124
        if($createIfNotExists && !$this->fileSystem->nodeExists($configPath)) {
125
            $this->fileSystem->newFile($configPath);
126
            $this->write($configPath);
127
128
        } else {
129
130
            $content = $this->fileSystem->get($configPath)->getContent();
131
            $configValues = parse_ini_string($content);
132
133
            if($configValues === false || count($configValues) === 0) {
134
                $this->logger->warning(
135
                    'Configuration invalid. Ignoring values.',
136
                    $this->loggerParams
137
                );
138
            } else {
139
140
                foreach($configValues as $key => $value) {
141
                    if(property_exists($this, $key)) {
142
                        $type = gettype($this->$key);
143
                        settype($value, $type);
144
                        $this->$key = $value;
145
                    } else {
146
                        $this->logger->warning(
147
                            'Configuration value "' . $key .
148
                            '" does not exist. Ignored value.' ,
149
                            $this->loggerParams
150
                        );
151
                    }
152
                }
153
154
            }
155
        }
156
    }
157
158
159
    public function write($configPath) {
160
        $ini =
161
            'autoPurgeMinimumInterval = ' .
162
                $this->autoPurgeMinimumInterval . "\n" .
163
            'autoPurgeCount = ' .
164
                $this->autoPurgeCount . "\n" .
165
            'maxRedirects = ' .
166
                $this->maxRedirects . "\n" .
167
            'maxSize = ' .
168
                $this->maxSize . "\n" .
169
            'exploreUrl = ' .
170
                $this->exploreUrl . "\n" .
171
            'feedFetcherTimeout = ' .
172
                $this->feedFetcherTimeout . "\n" .
173
            'useCronUpdates = ' .
174
                var_export($this->useCronUpdates, true);
175
        ;
176
177
        $this->fileSystem->get($configPath)->putContent($ini);
178
    }
179
180
181
}
182