DailyScan::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Http\Controllers\ScanController;
6
use App\Token;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\DB;
9
use Log;
10
11
class DailyScan extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'siwecos:dailyscan';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Starts a scan for each activated domain';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        // Get domains which are due to be scanned
45
        // Longest waiting first.
46
        $domains = DB::select(DB::raw(<<<'QUERY'
47
        select domain, token from domains
48
        left outer join (
49
               select url as domain
50
                    , max(created_at) as last_scan
51
               from scans
52
               where recurrentscan
53
               group by url
54
        ) LS
55
        using(domain)
56
        join(tokens)
57
        on(token_id=tokens.id)
58
        where verified
59
        and (
60
               last_scan is null
61
               or
62
               timestampdiff(DAY, last_scan, utc_timestamp()) > 0
63
        )
64
        order by last_scan asc
65
QUERY
66
        ));
67
        $max_schedule = min(env('RECURRENT_PER_RUN'), \count($domains));
68
        Log::info('Max Schedule: '.$max_schedule);
69
        /** @var string $domain */
70
        $bar = $this->output->createProgressBar($max_schedule);
71
        // If RECURRENT_PER_RUN is defined and > 0 this many scans are started
72
        // per run
73
        foreach ($domains as $domain) {
74
            ScanController::startScanJob(Token::whereToken($domain->token)->first(), $domain->domain, true, 10);
75
            $this->info('Scan started for: '.$domain->domain);
76
            $bar->advance();
77
            // no more scans are allowed to be started
78
            if (--$max_schedule == 0) {
79
                break;
80
            }
81
        }
82
        $bar->finish();
83
    }
84
}
85