Completed
Pull Request — master (#133)
by Victor
03:28
created

AppConfig::setAppValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
		'av_max_file_size' => -1,
49
		'av_stream_max_length' => '26214400',
50
		'av_infected_action' => 'only_log',
51
	];
52
53
	/**
54
	 * AppConfig constructor.
55
	 *
56
	 * @param IConfig $config
57
	 */
58 6
	public function __construct(IConfig $config) {
59 6
		$this->config = $config;
60 6
	}
61
62 5
	public function getAvChunkSize(){
63
		// See http://php.net/manual/en/function.stream-wrapper-register.php#74765
64
		// and \OC_Helper::streamCopy
65 5
		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
	/**
93
	 * Get all setting values as an array
94
	 * @return array
95
	 */
96
	public function getAllValues() {
97
		$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 3
	public function getAppValue($key) {
109 3
		$defaultValue = null;
110 3
		if (array_key_exists($key, $this->defaults)){
111 3
			$defaultValue = $this->defaults[$key];
112
		}
113 3
		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
	 * @throws \BadFunctionCallException
131
	 */
132
	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 3
	protected function getter($key) {
147 3
		if (array_key_exists($key, $this->defaults)) {
148 3
			return $this->getAppValue($key);
149
		} else {
150
			throw new \BadFunctionCallException($key . ' is not a valid key');
151
		}
152
	}
153
	
154
	/**
155
	 * Translates property_name into propertyName
156
	 * @param string $property
157
	 * @return string
158
	 */
159
	protected function camelCase($property){
160
		$split = explode('_', $property);
161
		$ucFirst = implode('', array_map('ucfirst', $split));
162
		$camelCase = lcfirst($ucFirst);
163
		return $camelCase;
164
	}
165
	
166
	/**
167
	 * Does all the someConfig to some_config magic
168
	 * @param string $property
169
	 * @return string
170
	 */
171 3
	protected function propertyToKey($property){
172 3
		$parts = preg_split('/(?=[A-Z])/', $property);
173 3
		$column = null;
174
175 3
		foreach($parts as $part){
176 3
			if($column === null){
177 3
				$column = $part;
178
			} else {
179 3
				$column .= '_' . lcfirst($part);
180
			}
181
		}
182
183 3
		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 3
	public function __call($methodName, $args){
194 3
		$attr = lcfirst( substr($methodName, 3) );
195 3
		$key = $this->propertyToKey($attr);
196 3
		if(strpos($methodName, 'set') === 0){
197
			$this->setter($key, $args);
198 3
		} elseif(strpos($methodName, 'get') === 0) {
199 3
			return $this->getter($key);
200
		} else {
201
			throw new \BadFunctionCallException($methodName . 
202
					' does not exist');
203
		}
204
	}
205
}
206