| Total Complexity | 46 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ScanController 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 ScanController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ScanController extends Controller |
||
| 19 | { |
||
| 20 | public function start(ScannerStartRequest $request) |
||
| 21 | { |
||
| 22 | $token = Token::getTokenByString(($request->header('siwecosToken'))); |
||
| 23 | |||
| 24 | Log::info('Token: '.$token->token); |
||
| 25 | if ($token instanceof Token && $token->reduceCredits()) { |
||
| 26 | $isNotTestRunner = $request->json('isNotATest') ?? true; |
||
| 27 | $dangerlevel = $request->json('dangerLevel') ?? 10; |
||
| 28 | |||
| 29 | return self::startScanJob($token, $request->json('domain'), false, $dangerlevel, $isNotTestRunner); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function startScanJob(Token $token, string $domain, bool $isRecurrent = false, int $dangerLevel = 0, bool $isRegistered = false) |
||
| 34 | { |
||
| 35 | |||
| 36 | // create a new scan order |
||
| 37 | /** @var Domain $currentDomain */ |
||
| 38 | $currentDomain = Domain::getDomainOrFail($domain, $token->id); |
||
| 39 | /** @var Scan $scan */ |
||
| 40 | $scan = $token->scans()->create([ |
||
| 41 | 'token_id' => $token->id, |
||
| 42 | 'url' => $currentDomain->domain, |
||
| 43 | 'callbackurls' => [], |
||
| 44 | 'dangerLevel' => $dangerLevel, |
||
| 45 | 'recurrentscan' => $isRecurrent, |
||
| 46 | ]); |
||
| 47 | |||
| 48 | $scan->recurrentscan = $isRecurrent ? 1 : 0; |
||
| 49 | $scan->save(); |
||
| 50 | |||
| 51 | $envVars = array_key_exists('APP_NAME', $_ENV) ? $_ENV : getenv(); |
||
| 52 | |||
| 53 | // dispatch each scanner to the queue |
||
| 54 | foreach ($envVars as $key => $value) { |
||
| 55 | Log::info($key.' '.$value); |
||
| 56 | if (!preg_match("/^SCANNER_(\w+)_URL$/", $key, $scanner_name)) { |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | if (!preg_match("/^https?:\/\//", $value)) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | ScanJob::dispatch($scanner_name[1], $value, $scan); |
||
| 63 | } |
||
| 64 | |||
| 65 | return response()->json(new ScanStatusResponse($scan)); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function GetResultById(int $id) |
||
| 69 | { |
||
| 70 | $scan = Scan::find($id); |
||
| 71 | |||
| 72 | return response()->json(new ScanRawResultResponse($scan)); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function GetStatusById(int $id) |
||
| 76 | { |
||
| 77 | $scan = Scan::find($id); |
||
| 78 | |||
| 79 | return response()->json(new ScanStatusResponse($scan)); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function status(Request $request) |
||
| 83 | { |
||
| 84 | // $token = Token::getTokenByString( ( $request->header( 'siwecosToken' ) ) ); |
||
| 85 | // $domain = Domain::getDomainOrFail( $request->get( 'url'), $token->id ); |
||
| 86 | $domain = Domain::whereDomain($request->get('url'))->first(); |
||
| 87 | $scan = Scan::whereUrl($domain->domain)->latest()->first(); |
||
| 88 | if ($scan instanceof Scan) { |
||
| 89 | return response()->json(new ScanStatusResponse($scan)); |
||
| 90 | } |
||
| 91 | |||
| 92 | return response('No results found', 422); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function result(Request $request) |
||
| 97 | // to be implemented |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Request $request |
||
| 102 | * |
||
| 103 | * @return Scan |
||
| 104 | */ |
||
| 105 | public function startFreeScan(Request $request) |
||
| 106 | { |
||
| 107 | $domainFilter = parse_url($request->json('domain')); |
||
| 108 | $domain = $domainFilter['scheme'].'://'.$domainFilter['host']; |
||
| 109 | |||
| 110 | //PING THE GIVEN DOMAIN |
||
| 111 | if (!self::isDomainAlive($domain)) { |
||
| 112 | Log::info('Domain not found '.$domain); |
||
| 113 | |||
| 114 | return response('Domain not alive', 422); |
||
| 115 | } |
||
| 116 | |||
| 117 | Log::info('Start Freescan for:'.$domain); |
||
| 118 | /** @var Domain $freeScanDomain */ |
||
| 119 | $freeScanDomain = Domain::whereDomain($domain)->first(); |
||
| 120 | |||
| 121 | if ($freeScanDomain instanceof Domain) { |
||
| 122 | //Domain already taken or another freescan has taken |
||
| 123 | /* @var Scan $lastScan */ |
||
| 124 | // $lastScan = $freeScanDomain->scans()->get()->last(); |
||
| 125 | // if ( $lastScan instanceof Scan ) { |
||
| 126 | // // return minified Version |
||
| 127 | // return response()->json( new ScanStatusResponse( $lastScan ) ); |
||
| 128 | // } |
||
| 129 | |||
| 130 | return $this->startNewFreeScan($freeScanDomain); |
||
| 131 | } |
||
| 132 | $freeScanDomain = new Domain(['domain' => $domain]); |
||
| 133 | $freeScanDomain->save(); |
||
| 134 | |||
| 135 | return $this->startNewFreeScan($freeScanDomain); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Check if Domain is Alive or redirects (200 / 301). |
||
| 140 | * |
||
| 141 | * @param string $domain |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public static function isDomainAlive(string $domain) |
||
| 146 | { |
||
| 147 | $client = new Client(); |
||
| 148 | |||
| 149 | try { |
||
| 150 | $response = $client->get($domain); |
||
| 151 | if ($response->getStatusCode() === 200 || $response->getStatusCode() === 301) { |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | } catch (\Exception $ex) { |
||
| 155 | Log::info($domain.' '.$ex->getMessage()); |
||
| 156 | |||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | protected function startNewFreeScan(Domain $freeScanDomain) |
||
| 164 | { |
||
| 165 | // start Scan and Broadcast Result afterwards |
||
| 166 | /** @var Scan $scan */ |
||
| 167 | $scan = $freeScanDomain->scans()->create([ |
||
| 168 | 'url' => $freeScanDomain, |
||
| 169 | 'callbackurls' => [], |
||
| 170 | 'dangerLevel' => 0, |
||
| 171 | 'freescan' => true, |
||
| 172 | ]); |
||
| 173 | $scan->freescan = 1; |
||
| 174 | $scan->save(); |
||
| 175 | |||
| 176 | // dispatch each scanner to the queue |
||
| 177 | foreach ($_ENV as $key => $value) { |
||
| 178 | if (!preg_match("/^SCANNER_(\w+)_URL$/", $key, $scanner_name)) { |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | if (!preg_match("/^https?:\/\//", $value)) { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | ScanJob::dispatch($scanner_name[1], $value, $scan); |
||
| 185 | } |
||
| 186 | |||
| 187 | return response()->json(new ScanStatusResponse($scan)); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getLastScanDate(string $format, string $domain) |
||
| 205 | } |
||
| 206 | |||
| 207 | public function resultRaw(Request $request) |
||
| 222 | } |
||
| 223 | |||
| 224 | public function resultRawFree(Request $request) |
||
| 225 | { |
||
| 226 | $domain = Domain::whereDomain($request->get('domain'))->first(); |
||
| 238 | } |
||
| 239 | |||
| 240 | // TODO: Check and Test |
||
| 241 | public function callback(Request $request, int $scanId) |
||
| 280 | } |
||
| 281 | |||
| 282 | protected function updateScanStatus(Scan $scan) |
||
| 283 | { |
||
| 326 |