Passed
Pull Request — master (#10)
by
unknown
15:04
created

SecurityMailCommand::handle()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

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