ScanJob   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 33 3
A getErrorArray() 0 17 1
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Scan;
6
use App\ScanResult;
7
use Exception;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
use Illuminate\Bus\Queueable;
12
use Illuminate\Contracts\Queue\ShouldQueue;
13
use Illuminate\Foundation\Bus\Dispatchable;
14
use Illuminate\Queue\InteractsWithQueue;
15
use Illuminate\Queue\SerializesModels;
16
use Log;
17
18
class ScanJob implements ShouldQueue
19
{
20
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\ScanJob: $id, $class
Loading history...
21
22
    protected $scan;
23
    protected $name;
24
    protected $scanner_url;
25
26
    /**
27
     * Create a new job instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct(string $name, string $scanner_url, Scan $scan)
32
    {
33
        $this->scan = $scan;
34
        $this->name = $name;
35
        $this->scanner_url = $scanner_url;
36
    }
37
38
    /**
39
     * Execute the job.
40
     *
41
     * @return void
42
     */
43
    public function handle()
44
    {
45
        $this->scan->update([
46
            'status' => 2,
47
        ]);
48
49
        /** @var ScanResult $scanResult */
50
        $scanResult = $this->scan->results()->create([
51
            'scanner_type' => $this->name,
52
        ]);
53
54
        $callbackUrl = route('callback', ['scanId' => $scanResult->id]);
55
        Log::info('Callback Route'.$callbackUrl);
56
        $client = new Client();
57
        $request = new Request('POST', $this->scanner_url, ['content-type' => 'application/json'], \GuzzleHttp\json_encode([
58
            'url'          => $this->scan->url,
59
            'callbackurls' => [$callbackUrl],
60
            'dangerLevel'  => $this->scan->dangerLevel,
61
        ]));
62
        $response = $client->sendAsync($request);
63
64
        try {
65
            /** @var Response $promise */
66
            $promise = $response->wait();
67
            $status = $promise->getStatusCode();
68
            Log::info('StatusCode for '.$this->name.' ('.$scanResult->scan_id.'): '.$status);
69
            if ($status !== 200) {
70
                $scanResult->result = self::getErrorArray($this->scan, $status);
71
            }
72
        } catch (Exception $ex) {
73
            $scanResult->result = self::getErrorArray($this->scan, 500, $ex->getMessage());
74
            // only way to make it async
75
            Log::info($this->name.' has started');
76
        }
77
    }
78
79
    public static function getErrorArray(string $scanner, int $status, string $exception = '')
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    public static function getErrorArray(string $scanner, int $status, /** @scrutinizer ignore-unused */ string $exception = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        $timeout = [];
82
        $timeout['name'] = 'SCANNER_ERROR';
83
        $timeout['hasError'] = true;
84
        $timeout['dangerlevel'] = 0;
85
        $timeout['score'] = 0;
86
        $timeout['scoreType'] = 'success';
87
        $timeout['testDetails'] = [];
88
        $timeout['errorMessage'] = [];
89
        $timeout['errorMessage']['placeholder'] = 'SCANNER_ERROR';
90
        $timeout['errorMessage']['values'] = [];
91
        $timeout['errorMessage']['values']['scanner'] = $scanner;
92
        $timeout['errorMessage']['values']['statuscode'] = $status;
93
        $timeout['errorMessage']['values']['exception'] = $status;
94
95
        return [$timeout];
96
    }
97
}
98