1 | <?php |
||
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 | 7 | public function __construct(IConfig $config) { |
|
61 | |||
62 | 4 | public function getAvChunkSize(){ |
|
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() { |
||
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 | 3 | } |
|
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) { |
||
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) { |
|
153 | |||
154 | /** |
||
155 | * Translates property_name into propertyName |
||
156 | * @param string $property |
||
157 | * @return string |
||
158 | */ |
||
159 | protected function camelCase($property){ |
||
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 | 3 | } else { |
|
179 | 3 | $column .= '_' . lcfirst($part); |
|
180 | } |
||
181 | 3 | } |
|
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){ |
|
205 | } |
||
206 |