Completed
Push — master ( d944f0...12d1a2 )
by Jorijn
01:57
created

SecurityMailCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fire() 0 28 6
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
    public function __construct(SecurityChecker $checker)
33
    {
34
        parent::__construct();
35
36
        $this->checker = $checker;
37
    }
38
39
    /**
40
     * Fire the command
41
     */
42
    public function fire()
43
    {
44
        // get the path to composer.lock
45
        $composerLock = base_path('composer.lock');
46
47
        // and feed it into the SecurityChecker
48
        $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
        $proceed = config('laravel-security-checker.email_even_without_vulnerabilities', false);
53
        if (count($checkResult) === 0 && $proceed !== true) {
54
            return 0;
55
        }
56
57
        // get the recipients and filter out any configuration mistakes
58
        $recipients = collect(config('laravel-security-checker.recipients', [ ]))->filter(function ($recipient) {
59
            return !is_null($recipient) && !empty($recipient);
60
        });
61
62
        if (is_null($recipients) || count($recipients) === 0) {
63
            $this->error(trans('laravel-security-checker::messages.no_recipients_configured'));
0 ignored issues
show
Bug introduced by
It seems like trans('laravel-security-...recipients_configured') can also be of type Illuminate\Contracts\Translation\Translator and array; however, parameter $string of Illuminate\Console\Command::error() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
            $this->error(/** @scrutinizer ignore-type */ trans('laravel-security-checker::messages.no_recipients_configured'));
Loading history...
64
            return 1;
65
        }
66
67
        Mail::to($recipients->toArray())->send(new SecurityMail($checkResult));
68
69
        return 0;
70
    }
71
}
72