Completed
Pull Request — stable9 (#144)
by Victor
03:10
created

AppConfig::getAvChunkSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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