| Total Complexity | 40 |
| Total Lines | 346 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SysLogsManagementProcessor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SysLogsManagementProcessor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class SysLogsManagementProcessor extends Injectable |
||
| 37 | { |
||
| 38 | public const DEFAULT_FILENAME = 'asterisk/messages'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Processes syslog requests |
||
| 42 | * |
||
| 43 | * @param array $request |
||
| 44 | * |
||
| 45 | * @return PBXApiResult An object containing the result of the API call. |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | public static function callBack(array $request): PBXApiResult |
||
| 49 | { |
||
| 50 | $action = $request['action']; |
||
| 51 | $data = $request['data']; |
||
| 52 | $res = new PBXApiResult(); |
||
| 53 | $res->processor = __METHOD__; |
||
| 54 | switch ($action) { |
||
| 55 | case 'getLogFromFile': |
||
| 56 | $res = self::getLogFromFile($data['filename'], $data['filter'], $data['lines'], $data['offset']); |
||
| 57 | break; |
||
| 58 | case 'prepareLog': |
||
| 59 | $res = self::prepareLog(false); |
||
| 60 | $res->processor = $action; |
||
| 61 | break; |
||
| 62 | case 'startLog': |
||
| 63 | $res = self::startLog(); |
||
| 64 | break; |
||
| 65 | case 'stopLog': |
||
| 66 | $res = self::prepareLog(true); |
||
| 67 | $res->processor = $action; |
||
| 68 | break; |
||
| 69 | case 'downloadLogsArchive': |
||
| 70 | $res = self::downloadLogsArchive($data['filename']); |
||
| 71 | break; |
||
| 72 | case 'downloadLogFile': |
||
| 73 | $res = self::downloadLogFile($data['filename']); |
||
| 74 | break; |
||
| 75 | case 'getLogsList': |
||
| 76 | $res = self::getLogsList(); |
||
| 77 | break; |
||
| 78 | case 'eraseFile': |
||
| 79 | $res = self::eraseFile($data['filename']); |
||
| 80 | break; |
||
| 81 | default: |
||
| 82 | $res->messages['error'][] = "Unknown action - $action in ".__CLASS__; |
||
| 83 | } |
||
| 84 | |||
| 85 | $res->function = $action; |
||
| 86 | |||
| 87 | return $res; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Gets partially filtered log file strings. |
||
| 92 | * |
||
| 93 | * @param string $filename |
||
| 94 | * @param string $filter |
||
| 95 | * @param int $lines |
||
| 96 | * @param int $offset |
||
| 97 | * |
||
| 98 | * @return PBXApiResult An object containing the result of the API call. |
||
| 99 | */ |
||
| 100 | public static function getLogFromFile(string $filename, string $filter = '', $lines = 500, $offset = 0): PBXApiResult |
||
| 101 | { |
||
| 102 | $res = new PBXApiResult(); |
||
| 103 | $res->processor = __METHOD__; |
||
| 104 | $filename = System::getLogDir() . '/' . $filename; |
||
| 105 | if ( ! file_exists($filename)) { |
||
| 106 | $res->success = false; |
||
| 107 | $res->messages[] = 'No access to the file ' . $filename; |
||
| 108 | } else { |
||
| 109 | $res->success = true; |
||
| 110 | $head = Util::which('head'); |
||
| 111 | $grep = '/bin/grep'; |
||
| 112 | if (!is_executable($grep)) { |
||
| 113 | $grep = Util::which('grep'); |
||
| 114 | } |
||
| 115 | $tail = Util::which('tail'); |
||
| 116 | $filter = escapeshellarg($filter); |
||
| 117 | $offset = (int)$offset; |
||
| 118 | $lines = (int)$lines; |
||
| 119 | $linesPlusOffset = $lines+$offset; |
||
| 120 | |||
| 121 | $di = Di::getDefault(); |
||
| 122 | $dirsConfig = $di->getShared('config'); |
||
| 123 | $filenameTmp = $dirsConfig->path('www.downloadCacheDir') . '/' . __FUNCTION__ . '_' . time() . '.log'; |
||
| 124 | if (empty($filter)){ |
||
| 125 | $cmd = "{$tail} -n {$linesPlusOffset} {$filename}"; |
||
| 126 | } else { |
||
| 127 | $cmd = "{$grep} --text -h -e ".str_replace('&',"' -e '", $filter)." -F {$filename} | $tail -n {$linesPlusOffset}"; |
||
| 128 | } |
||
| 129 | if ($offset>0){ |
||
| 130 | $cmd .= " | {$head} -n {$lines}"; |
||
| 131 | } |
||
| 132 | $cmd .= " > $filenameTmp"; |
||
| 133 | |||
| 134 | Processes::mwExec("$cmd; chown www:www $filenameTmp"); |
||
| 135 | $res->data['cmd']=$cmd; |
||
| 136 | $res->data['filename'] = $filenameTmp; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $res; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Starts the collection of logs and captures TCP packets. |
||
| 144 | * |
||
| 145 | * @return PBXApiResult An object containing the result of the API call. |
||
| 146 | */ |
||
| 147 | private static function startLog(): PBXApiResult |
||
| 148 | { |
||
| 149 | $res = new PBXApiResult(); |
||
| 150 | $res->processor = __METHOD__; |
||
| 151 | $logDir = System::getLogDir(); |
||
| 152 | |||
| 153 | // TCP dump |
||
| 154 | $tcpDumpDir = "{$logDir}/tcpDump"; |
||
| 155 | Util::mwMkdir($tcpDumpDir); |
||
| 156 | $network = new Network(); |
||
| 157 | $arr_eth = $network->getInterfacesNames(); |
||
| 158 | $tcpdumpPath = Util::which('tcpdump'); |
||
| 159 | $timeout = 300; |
||
| 160 | foreach ($arr_eth as $eth) { |
||
| 161 | Processes::mwExecBgWithTimeout( |
||
| 162 | "{$tcpdumpPath} -i {$eth} -n -s 0 -vvv -w {$tcpDumpDir}/{$eth}.pcap", |
||
| 163 | $timeout, |
||
| 164 | "{$tcpDumpDir}/{$eth}_out.log" |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | $res->success = true; |
||
| 168 | |||
| 169 | return $res; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Stops tcpdump and starts creating a log files archive for download. |
||
| 174 | * |
||
| 175 | * @param bool $tcpdumpOnly Indicates whether to include only tcpdump logs. |
||
| 176 | * |
||
| 177 | * @return PBXApiResult An object containing the result of the API call. |
||
| 178 | */ |
||
| 179 | private static function prepareLog(bool $tcpdumpOnly): PBXApiResult |
||
| 180 | { |
||
| 181 | $res = new PBXApiResult(); |
||
| 182 | $res->processor = __METHOD__; |
||
| 183 | $di = Di::getDefault(); |
||
| 184 | $dirsConfig = $di->getShared('config'); |
||
| 185 | $temp_dir = $dirsConfig->path('core.tempDir'); |
||
| 186 | |||
| 187 | $prefix = $tcpdumpOnly?'tcpdump':'sys'; |
||
| 188 | $futureFileName = $temp_dir . '/log-'.$prefix.'-' . time() . '.zip'; |
||
| 189 | $res->data['filename'] = $futureFileName; |
||
| 190 | $res->success = true; |
||
| 191 | // Create background task |
||
| 192 | $merge_settings = []; |
||
| 193 | $merge_settings['result_file'] = $futureFileName; |
||
| 194 | $merge_settings['tcpdump_only'] = $tcpdumpOnly; |
||
| 195 | |||
| 196 | if($tcpdumpOnly){ |
||
| 197 | Processes::killByName('timeout'); |
||
| 198 | Processes::killByName('tcpdump'); |
||
| 199 | } |
||
| 200 | $settings_file = "{$temp_dir}/log-settings-".$prefix.".json"; |
||
| 201 | file_put_contents($settings_file, json_encode($merge_settings, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
||
| 202 | $phpPath = Util::which('php'); |
||
| 203 | $workerFilesMergerPath = Util::getFilePathByClassName(WorkerMakeLogFilesArchive::class); |
||
| 204 | Processes::mwExecBg("{$phpPath} -f {$workerFilesMergerPath} start '{$settings_file}'"); |
||
| 205 | |||
| 206 | return $res; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Requests a zipped archive containing logs and PCAP file |
||
| 211 | * Checks if archive ready it returns download link. |
||
| 212 | * |
||
| 213 | * @param string $resultFile |
||
| 214 | * |
||
| 215 | * @return PBXApiResult An object containing the result of the API call. |
||
| 216 | */ |
||
| 217 | private static function downloadLogsArchive(string $resultFile): PBXApiResult |
||
| 218 | { |
||
| 219 | $res = new PBXApiResult(); |
||
| 220 | $res->processor = __METHOD__; |
||
| 221 | |||
| 222 | $progress_file = "{$resultFile}.progress"; |
||
| 223 | if ( !file_exists($progress_file)) { |
||
| 224 | $res->messages[] = 'Archive does not exist. Try again!'; |
||
| 225 | } elseif (file_exists($progress_file) && file_get_contents($progress_file) === '100') { |
||
| 226 | $uid = Util::generateRandomString(36); |
||
| 227 | $di = Di::getDefault(); |
||
| 228 | $downloadLink = $di->getShared('config')->path('www.downloadCacheDir'); |
||
| 229 | $result_dir = "{$downloadLink}/{$uid}"; |
||
| 230 | Util::mwMkdir($result_dir); |
||
| 231 | $link_name = 'MikoPBXLogs_' . basename($resultFile); |
||
| 232 | Util::createUpdateSymlink($resultFile, "{$result_dir}/{$link_name}"); |
||
| 233 | Util::addRegularWWWRights("{$result_dir}/{$link_name}"); |
||
| 234 | $res->success = true; |
||
| 235 | $res->data['status'] = "READY"; |
||
| 236 | $res->data['filename'] = "{$uid}/{$link_name}"; |
||
| 237 | } else { |
||
| 238 | $res->success = true; |
||
| 239 | $res->data['status'] = "PREPARING"; |
||
| 240 | $res->data['progress'] = file_get_contents($progress_file); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $res; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Prepares a downloadable link for a log file with the provided name. |
||
| 248 | * |
||
| 249 | * @param string $filename The name of the log file. |
||
| 250 | * |
||
| 251 | * @return PBXApiResult An object containing the result of the API call. |
||
| 252 | * |
||
| 253 | */ |
||
| 254 | private static function downloadLogFile(string $filename): PBXApiResult |
||
| 255 | { |
||
| 256 | $res = new PBXApiResult(); |
||
| 257 | $res->processor = __METHOD__; |
||
| 258 | $filename = System::getLogDir() . '/' . $filename; |
||
| 259 | if ( ! file_exists($filename)) { |
||
| 260 | $res->success = false; |
||
| 261 | $res->messages[] = 'File does not exist ' . $filename; |
||
| 262 | } else { |
||
| 263 | $uid = Util::generateRandomString(36); |
||
| 264 | $di = Di::getDefault(); |
||
| 265 | $downloadLink = $di->getShared('config')->path('www.downloadCacheDir'); |
||
| 266 | $result_dir = "{$downloadLink}/{$uid}"; |
||
| 267 | Util::mwMkdir($result_dir); |
||
| 268 | $link_name = basename($filename); |
||
| 269 | $lnPath = Util::which('ln'); |
||
| 270 | $chownPath = Util::which('chown'); |
||
| 271 | Processes::mwExec("{$lnPath} -s {$filename} {$result_dir}/{$link_name}"); |
||
| 272 | Processes::mwExec("{$chownPath} www:www {$result_dir}/{$link_name}"); |
||
| 273 | $res->success = true; |
||
| 274 | $res->data['filename'] = "{$uid}/{$link_name}"; |
||
| 275 | } |
||
| 276 | |||
| 277 | return $res; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Erase log file with the provided name. |
||
| 282 | * |
||
| 283 | * @param string $filename The name of the log file. |
||
| 284 | * |
||
| 285 | * @return PBXApiResult An object containing the result of the API call. |
||
| 286 | * |
||
| 287 | */ |
||
| 288 | private static function eraseFile(string $filename): PBXApiResult |
||
| 289 | { |
||
| 290 | $res = new PBXApiResult(); |
||
| 291 | $res->processor = __METHOD__; |
||
| 292 | $filename = System::getLogDir() . '/' . $filename; |
||
| 293 | if ( ! file_exists($filename)) { |
||
| 294 | $res->success = false; |
||
| 295 | $res->messages[] = 'File does not exist ' . $filename; |
||
| 296 | } else { |
||
| 297 | $echoPath = Util::which('echo'); |
||
| 298 | Processes::mwExec("$echoPath ' ' > $filename"); |
||
| 299 | $res->success = true; |
||
| 300 | } |
||
| 301 | |||
| 302 | return $res; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns list of log files to show them on web interface |
||
| 307 | * |
||
| 308 | * @return PBXApiResult An object containing the result of the API call. |
||
| 309 | */ |
||
| 310 | private static function getLogsList(): PBXApiResult |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Scans a directory just like scandir(), only recursively |
||
| 356 | * returns a hierarchical array representing the directory structure |
||
| 357 | * |
||
| 358 | * @param string $dir directory to scan |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | private static function scanDirRecursively(string $dir): array |
||
| 382 | } |
||
| 383 | } |