Completed
Pull Request — master (#225)
by Victor
05:30 queued 04:01
created

AppConfig::getCmdline()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.125

Importance

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