Completed
Push — master ( d0f8dc...76d05f )
by Victor
14s
created

AppConfig::propertyToKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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