1 | <?php |
||
42 | class AppConfig { |
||
43 | private $appName = 'files_antivirus'; |
||
44 | |||
45 | /** |
||
46 | * @var IConfig |
||
47 | */ |
||
48 | private $config; |
||
49 | |||
50 | private $defaults = [ |
||
51 | 'av_mode' => 'executable', |
||
52 | 'av_socket' => '/var/run/clamav/clamd.ctl', |
||
53 | 'av_host' => '', |
||
54 | 'av_port' => '', |
||
55 | 'av_cmd_options' => '', |
||
56 | 'av_path' => '/usr/bin/clamscan', |
||
57 | 'av_max_file_size' => -1, |
||
58 | 'av_stream_max_length' => '26214400', |
||
59 | 'av_infected_action' => 'only_log', |
||
60 | 'av_scan_background' => 'true', |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * AppConfig constructor. |
||
65 | * |
||
66 | * @param IConfig $config |
||
67 | */ |
||
68 | 12 | public function __construct(IConfig $config) { |
|
71 | |||
72 | 5 | public function getAvChunkSize() { |
|
77 | |||
78 | /** |
||
79 | * Get full commandline |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 1 | public function getCmdline() { |
|
84 | 1 | $avCmdOptions = $this->getAvCmdOptions(); |
|
85 | |||
86 | 1 | $shellArgs = []; |
|
87 | 1 | if ($avCmdOptions) { |
|
88 | $shellArgs = \explode(',', $avCmdOptions); |
||
89 | $shellArgs = \array_map( |
||
90 | function ($i) { |
||
91 | return \escapeshellarg($i); |
||
92 | }, |
||
93 | $shellArgs |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | 1 | $preparedArgs = ''; |
|
98 | 1 | if (\count($shellArgs)) { |
|
99 | $preparedArgs = \implode(' ', $shellArgs); |
||
100 | } |
||
101 | 1 | return $preparedArgs; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get all setting values as an array |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 3 | public function getAllValues() { |
|
115 | |||
116 | /** |
||
117 | * Get a value by key |
||
118 | * |
||
119 | * @param string $key |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 4 | public function getAppValue($key) { |
|
124 | 4 | $defaultValue = null; |
|
125 | 4 | if (\array_key_exists($key, $this->defaults)) { |
|
126 | 4 | $defaultValue = $this->defaults[$key]; |
|
127 | 4 | } |
|
128 | 4 | return $this->config->getAppValue($this->appName, $key, $defaultValue); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Set a value by key |
||
133 | * |
||
134 | * @param string $key |
||
135 | * @param string $value |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 1 | public function setAppValue($key, $value) { |
|
142 | |||
143 | /** |
||
144 | * Set a value with magic __call invocation |
||
145 | * |
||
146 | * @param string $key |
||
147 | * @param array $args |
||
148 | * |
||
149 | * @throws \BadFunctionCallException |
||
150 | */ |
||
151 | 1 | protected function setter($key, $args) { |
|
152 | 1 | if (\array_key_exists($key, $this->defaults)) { |
|
153 | 1 | $this->setAppValue($key, $args[0]); |
|
154 | 1 | } else { |
|
155 | throw new \BadFunctionCallException($key . ' is not a valid key'); |
||
156 | } |
||
157 | 1 | } |
|
158 | |||
159 | /** |
||
160 | * Get a value with magic __call invocation |
||
161 | * |
||
162 | * @param string $key |
||
163 | * |
||
164 | * @return string |
||
165 | * |
||
166 | * @throws \BadFunctionCallException |
||
167 | */ |
||
168 | 8 | protected function getter($key) { |
|
175 | |||
176 | /** |
||
177 | * Translates property_name into propertyName |
||
178 | * |
||
179 | * @param string $property |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | 3 | protected function camelCase($property) { |
|
189 | |||
190 | /** |
||
191 | * Does all the someConfig to some_config magic |
||
192 | * |
||
193 | * @param string $property |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 11 | protected function propertyToKey($property) { |
|
198 | 11 | $parts = \preg_split('/(?=[A-Z])/', $property); |
|
199 | 11 | $column = null; |
|
200 | |||
201 | 11 | foreach ($parts as $part) { |
|
202 | 11 | if ($column === null) { |
|
203 | 11 | $column = $part; |
|
204 | 11 | } else { |
|
205 | 11 | $column .= '_' . \lcfirst($part); |
|
206 | } |
||
207 | 11 | } |
|
208 | |||
209 | 11 | return $column; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get/set an option value by calling getSomeOption method |
||
214 | * |
||
215 | * @param string $methodName |
||
216 | * @param array $args |
||
217 | * |
||
218 | * @return string|null |
||
219 | * |
||
220 | * @throws \BadFunctionCallException |
||
221 | */ |
||
222 | 11 | public function __call($methodName, $args) { |
|
235 | } |
||
236 |