Completed
Push — master ( 28c19e...a4e9ca )
by Joas
04:02
created

AppConfig::getCmdline()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.365

Importance

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