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 | 12 | public function __construct(IConfig $config) { |
|
61 | |||
62 | 5 | public function getAvChunkSize(){ |
|
67 | |||
68 | /** |
||
69 | * Get full commandline |
||
70 | * @return string |
||
71 | */ |
||
72 | 1 | public function getCmdline(){ |
|
73 | 1 | $avCmdOptions = $this->getAvCmdOptions(); |
|
74 | |||
75 | 1 | $shellArgs = []; |
|
76 | 1 | if ($avCmdOptions) { |
|
77 | $shellArgs = explode(',', $avCmdOptions); |
||
78 | $shellArgs = array_map(function($i){ |
||
79 | return escapeshellarg($i); |
||
80 | }, |
||
81 | $shellArgs |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | 1 | $preparedArgs = ''; |
|
86 | 1 | if (count($shellArgs)){ |
|
87 | $preparedArgs = implode(' ', $shellArgs); |
||
88 | } |
||
89 | 1 | 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 | 4 | public function getAppValue($key) { |
|
109 | 4 | $defaultValue = null; |
|
110 | 4 | if (array_key_exists($key, $this->defaults)){ |
|
111 | 4 | $defaultValue = $this->defaults[$key]; |
|
112 | 4 | } |
|
113 | 4 | 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 | 1 | public function setAppValue($key, $value) { |
|
123 | 1 | return $this->config->setAppValue($this->appName, $key, $value); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Set a value with magic __call invocation |
||
128 | * @param string $key |
||
129 | * @param array $args |
||
130 | * @throws \BadFunctionCallException |
||
131 | */ |
||
132 | 1 | protected function setter($key, $args) { |
|
133 | 1 | if (array_key_exists($key, $this->defaults)) { |
|
134 | 1 | $this->setAppValue($key, $args[0]); |
|
135 | 1 | } else { |
|
136 | throw new \BadFunctionCallException($key . ' is not a valid key'); |
||
137 | } |
||
138 | 1 | } |
|
139 | |||
140 | /** |
||
141 | * Get a value with magic __call invocation |
||
142 | * @param string $key |
||
143 | * @return string |
||
144 | * @throws \BadFunctionCallException |
||
145 | */ |
||
146 | 8 | 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 | 8 | protected function propertyToKey($property){ |
|
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 | 8 | public function __call($methodName, $args){ |
|
205 | } |
||
206 |