Passed
Pull Request — master (#34)
by Paras
14:04
created

SecurityMailCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 22
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 4
b 1
f 1
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 36 5
1
<?php
2
3
namespace Jorijn\LaravelSecurityChecker\Console;
4
5
use Enlightn\SecurityChecker\AdvisoryAnalyzer;
6
use Enlightn\SecurityChecker\AdvisoryFetcher;
7
use Enlightn\SecurityChecker\AdvisoryParser;
8
use Enlightn\SecurityChecker\Composer;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Mail;
12
use Jorijn\LaravelSecurityChecker\Mailables\SecurityMail;
13
14
class SecurityMailCommand extends Command
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $name = 'security-check:email';
20
21
    /**
22
     * @var string
23
     */
24
    protected $description = 'Emails any vulnerabilities for packages you have in your composer.lock file.';
25
26
    /**
27
     * Execute the command
28
     */
29
    public function handle()
30
    {
31
        // get the path to composer.lock
32
        $composerLock = base_path('composer.lock');
33 27
34
        // and feed it into the SecurityChecker
35 27
        Log::debug('about to check for vulnerabilities');
36
        $parser = new AdvisoryParser((new AdvisoryFetcher)->fetchAdvisories());
37 27
        $dependencies = (new Composer)->getDependencies($composerLock);
38 27
        $checkResult = (new AdvisoryAnalyzer($parser->getAdvisories()))->analyzeDependencies($dependencies);
39
40
        // if the user didn't want any email if there are no results,
41
        // cancel execution here.
42
        $proceed = config('laravel-security-checker.notify_even_without_vulnerabilities', false);
43 12
        if ($proceed !== true && \count($checkResult) === 0) {
44
            Log::info('no vulnerabilities were found, not sending any email');
45
            return 0;
46 12
        }
47
48
        // get the recipients and filter out any configuration mistakes
49 12
        $recipients = collect(config('laravel-security-checker.recipients', [ ]))->filter(function ($recipient) {
50 12
            return $recipient !== null && !empty($recipient);
51
        });
52
53
        if ($recipients->count() === 0) {
54 12
            Log::error('vulnerabilities were found, but there are no recipients configured');
55 12
            $this->error(
56 3
                /** @scrutinizer ignore-type */__('laravel-security-checker::messages.no_recipients_configured')
57 3
            );
58
            return 1;
59
        }
60
61 9
        Log::warning('vulnerabilities were found, emailed to configured recipients');
62 6
        Mail::to($recipients->toArray())->send(new SecurityMail($checkResult));
63 9
64
        return 0;
65 9
    }
66
}
67