Completed
Push — master ( 46dc92...267b49 )
by Victor
13s queued 11s
created

AppConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * ownCloud - Files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2015-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus;
15
16
use \OCP\IConfig;
17
18
	/**
19
	 * @method string getAvMode()
20
	 * @method string getAvSocket()
21
	 * @method string getAvHost()
22
	 * @method int getAvPort()
23
	 * @method int getAvMaxFileSize()
24
	 * @method int getAvStreamMaxLength()
25
	 * @method string getAvCmdOptions()
26
	 * @method string getAvPath()
27
	 * @method string getAvInfectedAction()
28
	 * @method string getAvScanBackground()
29
	 *
30
	 * @method null setAvMode(string $avMode)
31
	 * @method null setAvSocket(string $avsocket)
32
	 * @method null setAvHost(string $avHost)
33
	 * @method null setAvPort(int $avPort)
34
	 * @method null setAvMaxFileSize(int $fileSize)
35
	 * @method null setAvStreamMaxLength(int $streamMaxLength)
36
	 * @method null setAvCmdOptions(string $avCmdOptions)
37
	 * @method null setAvPath(string $avPath)
38
	 * @method null setAvInfectedAction(string $avInfectedAction)
39
	 * @method null setAvScanBackground(string $scanBackground)
40
	 */
41
42
class AppConfig {
43
	private $appName = 'files_antivirus';
44
45
	/**
46
	 * @var IConfig
47
	 */
48
	private $config;
49
50
	private $defaults = [
51
		'av_mode' => 'executable',
52
		'av_socket' => '/var/run/clamav/clamd.ctl',
53
		'av_host' => '',
54
		'av_port' => '',
55
		'av_cmd_options' => '',
56
		'av_path' => '/usr/bin/clamscan',
57
		'av_max_file_size' => -1,
58
		'av_stream_max_length' => '26214400',
59
		'av_infected_action' => 'only_log',
60
		'av_scan_background' => 'true',
61
	];
62
63
	/**
64
	 * AppConfig constructor.
65
	 *
66
	 * @param IConfig $config
67
	 */
68 12
	public function __construct(IConfig $config) {
69 12
		$this->config = $config;
70 12
	}
71
72 5
	public function getAvChunkSize() {
73
		// See http://php.net/manual/en/function.stream-wrapper-register.php#74765
74
		// and \OC_Helper::streamCopy
75 5
		return 8192;
76
	}
77
	
78
	/**
79
	 * Get full commandline
80
	 *
81
	 * @return string
82
	 */
83 1
	public function getCmdline() {
84 1
		$avCmdOptions = $this->getAvCmdOptions();
85
		
86 1
		$shellArgs = [];
87 1
		if ($avCmdOptions) {
88
			$shellArgs = \explode(',', $avCmdOptions);
89
			$shellArgs = \array_map(
90
				function ($i) {
91
					return \escapeshellarg($i);
92
				},
93
				$shellArgs
94
			);
95
		}
96
		
97 1
		$preparedArgs = '';
98 1
		if (\count($shellArgs)) {
99
			$preparedArgs = \implode(' ', $shellArgs);
100
		}
101 1
		return $preparedArgs;
102
	}
103
	
104
	/**
105
	 * Get all setting values as an array
106
	 *
107
	 * @return array
108
	 */
109 3
	public function getAllValues() {
110 3
		$keys = \array_keys($this->defaults);
111 3
		$values = \array_map([$this, 'getAppValue'], $keys);
112 3
		$preparedKeys = \array_map([$this, 'camelCase'], $keys);
113 3
		return \array_combine($preparedKeys, $values);
114
	}
115
	
116
	/**
117
	 * Get a value by key
118
	 *
119
	 * @param string $key
120
	 *
121
	 * @return string
122
	 */
123 4
	public function getAppValue($key) {
124 4
		$defaultValue = null;
125 4
		if (\array_key_exists($key, $this->defaults)) {
126 4
			$defaultValue = $this->defaults[$key];
127
		}
128 4
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
129
	}
130
131
	/**
132
	 * Set a value by key
133
	 *
134
	 * @param string $key
135
	 * @param string $value
136
	 *
137
	 * @return string
138
	 */
139 1
	public function setAppValue($key, $value) {
140 1
		return $this->config->setAppValue($this->appName, $key, $value);
141
	}
142
	
143
	/**
144
	 * Set a value with magic __call invocation
145
	 *
146
	 * @param string $key
147
	 * @param array $args
148
	 *
149
	 * @throws \BadFunctionCallException
150
	 */
151 1
	protected function setter($key, $args) {
152 1
		if (\array_key_exists($key, $this->defaults)) {
153 1
			$this->setAppValue($key, $args[0]);
154
		} else {
155
			throw new \BadFunctionCallException($key . ' is not a valid key');
156
		}
157 1
	}
158
159
	/**
160
	 * Get a value with magic __call invocation
161
	 *
162
	 * @param string $key
163
	 *
164
	 * @return string
165
	 *
166
	 * @throws \BadFunctionCallException
167
	 */
168 8
	protected function getter($key) {
169 8
		if (\array_key_exists($key, $this->defaults)) {
170 8
			return $this->getAppValue($key);
171
		} else {
172
			throw new \BadFunctionCallException($key . ' is not a valid key');
173
		}
174
	}
175
	
176
	/**
177
	 * Translates property_name into propertyName
178
	 *
179
	 * @param string $property
180
	 *
181
	 * @return string
182
	 */
183 3
	protected function camelCase($property) {
184 3
		$split = \explode('_', $property);
185 3
		$ucFirst = \implode('', \array_map('ucfirst', $split));
186 3
		$camelCase = \lcfirst($ucFirst);
187 3
		return $camelCase;
188
	}
189
	
190
	/**
191
	 * Does all the someConfig to some_config magic
192
	 *
193
	 * @param string $property
194
	 *
195
	 * @return string
196
	 */
197 11
	protected function propertyToKey($property) {
198 11
		$parts = \preg_split('/(?=[A-Z])/', $property);
199 11
		$column = null;
200
201 11
		foreach ($parts as $part) {
202 11
			if ($column === null) {
203 11
				$column = $part;
204
			} else {
205 11
				$column .= '_' . \lcfirst($part);
206
			}
207
		}
208
209 11
		return $column;
210
	}
211
	
212
	/**
213
	 * Get/set an option value by calling getSomeOption method
214
	 *
215
	 * @param string $methodName
216
	 * @param array $args
217
	 *
218
	 * @return string|null
219
	 *
220
	 * @throws \BadFunctionCallException
221
	 */
222 11
	public function __call($methodName, $args) {
223 11
		$attr = \lcfirst(\substr($methodName, 3));
224 11
		$key = $this->propertyToKey($attr);
225 11
		if (\strpos($methodName, 'set') === 0) {
226 4
			$this->setter($key, $args);
227 8
		} elseif (\strpos($methodName, 'get') === 0) {
228 8
			return $this->getter($key);
229
		} else {
230
			throw new \BadFunctionCallException(
231
				$methodName . ' does not exist'
232
			);
233
		}
234 4
	}
235
}
236