1 | <?php |
||
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) { |
|
64 | |||
65 | 5 | public function getAvChunkSize(){ |
|
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() { |
|
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 | 4 | } |
|
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) { |
|
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 | 1 | } 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) { |
|
156 | |||
157 | /** |
||
158 | * Translates property_name into propertyName |
||
159 | * @param string $property |
||
160 | * @return string |
||
161 | */ |
||
162 | 3 | protected function camelCase($property){ |
|
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 | 11 | } else { |
|
182 | 11 | $column .= '_' . lcfirst($part); |
|
183 | } |
||
184 | 11 | } |
|
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){ |
|
208 | } |
||
209 |