Completed
Pull Request — stable8.2 (#145)
by Victor
02:36
created

AppConfig   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 42.42%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 165
ccs 28
cts 66
cp 0.4242
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAllValues() 0 6 1
A setAppValue() 0 3 1
A getter() 0 7 2
A camelCase() 0 6 1
A __call() 0 12 3
A getCmdline() 0 19 3
A getAppValue() 0 7 2
A setter() 0 7 2
A propertyToKey() 0 14 3
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
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_chunk_size' => '1024',
48
		'av_path' => '/usr/bin/clamscan',
49
		'av_max_file_size' => -1,
50
		'av_infected_action' => 'only_log',
51
	];
52
53
	/**
54
	 * AppConfig constructor.
55
	 *
56
	 * @param IConfig $config
57
	 */
58 5
	public function __construct(IConfig $config) {
59 5
		$this->config = $config;
60 5
	}
61
	
62
	/**
63
	 * Get full commandline
64
	 * @return string
65
	 */
66
	public function getCmdline(){
67
		$avCmdOptions = $this->getAvCmdOptions();
68
		
69
		$shellArgs = [];
70
		if ($avCmdOptions) {
71
			$shellArgs = explode(',', $avCmdOptions);
72
				$shellArgs = array_map(function($i){
73
					return escapeshellarg($i);
74
				},
75
				$shellArgs
76
			);
77
		}
78
		
79
		$preparedArgs = '';
80
		if (count($shellArgs)){
81
			$preparedArgs = implode(' ', $shellArgs);
82
		}
83
		return $preparedArgs;
84
	}
85
	
86
	/**
87
	 * Get all setting values as an array
88
	 * @return array
89
	 */
90
	public function getAllValues() {
91
		$keys = array_keys($this->defaults);
92
		$values = array_map(array($this, 'getAppValue'), $keys);
93
		$preparedKeys = array_map(array($this, 'camelCase'), $keys);
94
		return array_combine($preparedKeys, $values);
95
	}
96
	
97
	/**
98
	 * Get a value by key
99
	 * @param string $key
100
	 * @return string
101
	 */
102 5
	public function getAppValue($key) {
103 5
		$defaultValue = null;
104 5
		if (array_key_exists($key, $this->defaults)){
105 5
			$defaultValue = $this->defaults[$key];
106 5
		}
107 5
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
108
	}
109
110
	/**
111
	 * Set a value by key
112
	 * @param string $key
113
	 * @param string $value
114
	 * @return string
115
	 */
116
	public function setAppValue($key, $value) {
117
		return $this->config->setAppValue($this->appName, $key, $value);
118
	}
119
	
120
	/**
121
	 * Set a value with magic __call invocation
122
	 * @param string $key
123
	 * @param array $args
124
	 * @throws \BadFunctionCallException
125
	 */
126
	protected function setter($key, $args) {
127
		if (array_key_exists($key, $this->defaults)) {
128
			$this->setAppValue($key, $args[0]);
129
		} else {
130
			throw new \BadFunctionCallException($key . ' is not a valid key');
131
		}
132
	}
133
134
	/**
135
	 * Get a value with magic __call invocation
136
	 * @param string $key
137
	 * @return string
138
	 * @throws \BadFunctionCallException
139
	 */
140 5
	protected function getter($key) {
141 5
		if (array_key_exists($key, $this->defaults)) {
142 5
			return $this->getAppValue($key);
143
		} else {
144
			throw new \BadFunctionCallException($key . ' is not a valid key');
145
		}
146
	}
147
	
148
	/**
149
	 * Translates property_name into propertyName
150
	 * @param string $property
151
	 * @return string
152
	 */
153
	protected function camelCase($property){
154
		$split = explode('_', $property);
155
		$ucFirst = implode('', array_map('ucfirst', $split));
156
		$camelCase = lcfirst($ucFirst);
157
		return $camelCase;
158
	}
159
	
160
	/**
161
	 * Does all the someConfig to some_config magic
162
	 * @param string $property
163
	 * @return string
164
	 */
165 5
	protected function propertyToKey($property){
166 5
		$parts = preg_split('/(?=[A-Z])/', $property);
167 5
		$column = null;
168
169 5
		foreach($parts as $part){
170 5
			if($column === null){
171 5
				$column = $part;
172 5
			} else {
173 5
				$column .= '_' . lcfirst($part);
174
			}
175 5
		}
176
177 5
		return $column;
178
	}
179
	
180
	/**
181
	 * Get/set an option value by calling getSomeOption method
182
	 * @param string $methodName
183
	 * @param array $args
184
	 * @return string|null
185
	 * @throws \BadFunctionCallException
186
	 */
187 5
	public function __call($methodName, $args){
188 5
		$attr = lcfirst( substr($methodName, 3) );
189 5
		$key = $this->propertyToKey($attr);
190 5
		if(strpos($methodName, 'set') === 0){
191
			$this->setter($key, $args);
192 5
		} elseif(strpos($methodName, 'get') === 0) {
193 5
			return $this->getter($key);
194
		} else {
195
			throw new \BadFunctionCallException($methodName . 
196
					' does not exist');
197
		}
198
	}
199
}
200