SecurityMailCommand::handle()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 3
nop 0
dl 0
loc 34
ccs 19
cts 19
cp 1
crap 5
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
namespace Jorijn\LaravelSecurityChecker\Console;
4
5
use Enlightn\SecurityChecker\SecurityChecker;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Mail;
9
use Jorijn\LaravelSecurityChecker\Mailables\SecurityMail;
10
11
class SecurityMailCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $name = 'security-check:email';
17
18
    /**
19
     * @var string
20
     */
21
    protected $description = 'Emails any vulnerabilities for packages you have in your composer.lock file.';
22
23
    /**
24
     * @var SecurityChecker
25
     */
26
    protected $checker;
27
28
    /**
29
     * SecurityCommand constructor.
30
     *
31
     * @param SecurityChecker $checker
32
     */
33 27
    public function __construct(SecurityChecker $checker)
34
    {
35 27
        parent::__construct();
36
37 27
        $this->checker = $checker;
38 27
    }
39
40
    /**
41
     * Execute the command
42
     */
43 12
    public function handle()
44
    {
45
        // get the path to composer.lock
46 12
        $composerLock = base_path('composer.lock');
47
48
        // and feed it into the SecurityChecker
49 12
        Log::debug('about to check for vulnerabilities');
50 12
        $checkResult = $this->checker->check($composerLock);
51
52
        // if the user didn't want any email if there are no results,
53
        // cancel execution here.
54 12
        $proceed = config('laravel-security-checker.notify_even_without_vulnerabilities', false);
55 12
        if ($proceed !== true && \count($checkResult) === 0) {
56 3
            Log::info('no vulnerabilities were found, not sending any email');
57 3
            return 0;
58
        }
59
60
        // get the recipients and filter out any configuration mistakes
61 9
        $recipients = collect(config('laravel-security-checker.recipients', [ ]))->filter(function ($recipient) {
62 6
            return $recipient !== null && !empty($recipient);
63 9
        });
64
65 9
        if ($recipients->count() === 0) {
66 3
            Log::error('vulnerabilities were found, but there are no recipients configured');
67 3
            $this->error(
68 3
                /** @scrutinizer ignore-type */__('laravel-security-checker::messages.no_recipients_configured')
69
            );
70 3
            return 1;
71
        }
72
73 6
        Log::warning('vulnerabilities were found, emailed to configured recipients');
74 6
        Mail::to($recipients->toArray())->send(new SecurityMail($checkResult));
75
76 6
        return 0;
77
    }
78
}
79