Completed
Pull Request — master (#15)
by Robin
02:22
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 3
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 string getAvCmdOptions()
19
 * @method int getAvChunkSize()
20
 * @method string getAvPath()
21
 * @method string getAvInfectedAction()
22
 * @method null setAvMode(string $avMode)
23
 * @method null setAvSocket(string $avsocket)
24
 * @method null setAvHost(string $avHost)
25
 * @method null setAvPort(int $avPort)
26
 * @method null setAvCmdOptions(string $avCmdOptions)
27
 * @method null setAvChunkSize(int $chunkSize)
28
 * @method null setAvPath(string $avPath)
29
 * @method null setAvInfectedAction(string $avInfectedAction)
30
 */
31
class AppConfig {
32
	private $appName = 'files_antivirus';
33
	private $config;
34
35
	private $defaults = array(
36
		'av_mode' => 'executable',
37
		'av_socket' => '/var/run/clamav/clamd.ctl',
38
		'av_host' => '',
39
		'av_port' => '',
40
		'av_cmd_options' => '',
41
		'av_chunk_size' => '1024',
42
		'av_path' => '/usr/bin/clamscan',
43
		'av_infected_action' => 'only_log',
44
	);
45
	
46 4
	public function __construct(IConfig $config) {
47 4
		$this->config = $config;
48 4
	}
49
	
50
	/**
51
	 * Get full commandline
52
	 * @return string
53
	 */
54 2
	public function getCmdline(){
55 2
		$avCmdOptions = $this->getAvCmdOptions();
56
		
57 2
		$shellArgs = array();
58 2
		if ($avCmdOptions) {
59
			$shellArgs = explode(',', $avCmdOptions);
60
				$shellArgs = array_map(function($i){
61
					return escapeshellarg($i);
62
				},
63
				$shellArgs
64
			);
65
		}
66
		
67 2
		$preparedArgs = '';
68 2
		if (count($shellArgs)){
69
			$preparedArgs = implode(' ', $shellArgs);
70
		}
71 2
		return $preparedArgs;
72
	}
73
	
74
	/**
75
	 * Get all setting values as an array
76
	 * @return array
77
	 */
78
	public function getAllValues() {
79
		$keys = array_keys($this->defaults);
80
		$values = array_map(array($this, 'getAppValue'), $keys);
81
		$preparedKeys = array_map(array($this, 'camelCase'), $keys);
82
		return array_combine($preparedKeys, $values);
83
	}
84
	
85
	/**
86
	 * Get a value by key
87
	 * @param string $key
88
	 * @return string
89
	 */
90 4
	public function getAppValue($key) {
91 4
		$defaultValue = null;
92 4
		if (array_key_exists($key, $this->defaults)){
93 4
			$defaultValue = $this->defaults[$key];
94 4
		}
95 4
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
96
	}
97
98
	/**
99
	 * Set a value by key
100
	 * @param string $key
101
	 * @param string $value
102
	 */
103
	public function setAppValue($key, $value) {
104
		$this->config->setAppValue($this->appName, $key, $value);
105
	}
106
	
107
	/**
108
	 * Set a value with magic __call invocation
109
	 * @param string $key
110
	 * @param array $args
111
	 * @throws \BadFunctionCallException
112
	 */
113
	protected function setter($key, $args) {
114
		if (array_key_exists($key, $this->defaults)) {
115
			$this->setAppValue($key, $args[0]);
116
		} else {
117
			throw new \BadFunctionCallException($key . ' is not a valid key');
118
		}
119
	}
120
121
	/**
122
	 * Get a value with magic __call invocation
123
	 * @param string $key
124
	 * @return string
125
	 * @throws \BadFunctionCallException
126
	 */
127 4
	protected function getter($key) {
128 4
		if (array_key_exists($key, $this->defaults)) {
129 4
			return $this->getAppValue($key);
130
		} else {
131
			throw new \BadFunctionCallException($key . ' is not a valid key');
132
		}
133
	}
134
	
135
	/**
136
	 * Translates property_name into propertyName
137
	 * @param string $property
138
	 * @return string
139
	 */
140
	protected function camelCase($property){
141
		$split = explode('_', $property);
142
		$ucFirst = implode('', array_map('ucfirst', $split));
143
		$camelCase = lcfirst($ucFirst);
144
		return $camelCase;
145
	}
146
	
147
	/**
148
	 * Does all the someConfig to some_config magic
149
	 * @param string $property
150
	 * @return string
151
	 */
152 4
	protected function propertyToKey($property){
153 4
		$parts = preg_split('/(?=[A-Z])/', $property);
154 4
		$column = null;
155
156 4
		foreach($parts as $part){
157 4
			if($column === null){
158 4
				$column = $part;
159 4
			} else {
160 4
				$column .= '_' . lcfirst($part);
161
			}
162 4
		}
163
164 4
		return $column;
165
	}
166
	
167
	/**
168
	 * Get/set an option value by calling getSomeOption method
169
	 * @param string $methodName
170
	 * @param array $args
171
	 * @return string|null
172
	 * @throws \BadFunctionCallException
173
	 */
174 4
	public function __call($methodName, $args){
175 4
		$attr = lcfirst( substr($methodName, 3) );
176 4
		$key = $this->propertyToKey($attr);
177 4
		if(strpos($methodName, 'set') === 0){
178
			$this->setter($key, $args);
179 4
		} elseif(strpos($methodName, 'get') === 0) {
180 4
			return $this->getter($key);
181
		} else {
182
			throw new \BadFunctionCallException($methodName . 
183
					' does not exist');
184
		}
185
	}
186
}
187