Completed
Push — master ( b1e025...a9dc14 )
by Jorijn
01:54
created

SecurityMailCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 6
c 3
b 1
f 1
dl 0
loc 60
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fire() 0 28 5
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
     * @var string
14
     */
15
    protected $name = 'security-check:email';
16
17
    /**
18
     * @var string
19
     */
20
    protected $description = 'Emails any vulnerabilities for packages you have in your composer.lock file.';
21
22
    /**
23
     * @var SecurityChecker
24
     */
25
    protected $checker;
26
27
    /**
28
     * SecurityCommand constructor.
29
     *
30
     * @param SecurityChecker $checker
31
     */
32 15
    public function __construct(SecurityChecker $checker)
33
    {
34 15
        parent::__construct();
35
36 15
        $this->checker = $checker;
37 15
    }
38
39
    /**
40
     * Fire the command
41
     */
42 12
    public function fire()
43
    {
44
        // get the path to composer.lock
45 12
        $composerLock = base_path('composer.lock');
46
47
        // and feed it into the SecurityChecker
48 12
        $checkResult = $this->checker->check($composerLock);
49
50
        // if the user didn't want any email if there are no results,
51
        // cancel execution here.
52 12
        $proceed = config('laravel-security-checker.email_even_without_vulnerabilities', false);
53 12
        if (count($checkResult) === 0 && $proceed !== true) {
54 3
            return 0;
55
        }
56
57
        // get the recipients and filter out any configuration mistakes
58 9
        $recipients = collect(config('laravel-security-checker.recipients', [ ]))->filter(function ($recipient) {
59 6
            return !is_null($recipient) && !empty($recipient);
60 9
        });
61
62 9
        if ($recipients->count() === 0) {
63 3
            $this->error(__('laravel-security-checker::messages.no_recipients_configured'));
64 3
            return 1;
65
        }
66
67 6
        Mail::to($recipients->toArray())->send(new SecurityMail($checkResult));
68
69 6
        return 0;
70
    }
71
}
72