SecuritySlackCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Notification;
9
use Jorijn\LaravelSecurityChecker\Notifications\SecuritySlackNotification;
10
11
class SecuritySlackCommand extends Command
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $name = 'security-check:slack';
17
18
    /**
19
     * @var string
20
     */
21
    protected $description = 'Send vulnerabilities to a Slack channel.';
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 9
    public function handle()
44
    {
45
        // require that the user specifies a slack channel in the .env file
46 9
        if (!config('laravel-security-checker.slack_webhook_url')) {
47 3
            Log::error('checking for vulnerabilities using slack was requested but no hook is configured');
48 3
            throw new \Exception('No Slack Webhook has been specified.');
49
        }
50
51
        // get the path to composer.lock
52 6
        $composerLock = base_path('composer.lock');
53
54
        // and feed it into the SecurityChecker
55 6
        Log::debug('about to check for vulnerabilities');
56 6
        $vulnerabilities = $this->checker->check($composerLock);
57
58
        // cancel execution here if user does not want to be notified when there are 0 vulns.
59 6
        $proceed = config('laravel-security-checker.notify_even_without_vulnerabilities', false);
60 6
        if (count($vulnerabilities) === 0 && $proceed !== true) {
61 3
            Log::info('no vulnerabilities were found, not sending a slack notification');
62
63 3
            return 0;
64
        }
65
66 3
        Log::warning('vulnerabilities were found, sending slack notification to configured hook');
67 3
        Notification::route('slack', config('laravel-security-checker.slack_webhook_url', null))
68 3
            ->notify(new SecuritySlackNotification($vulnerabilities, realpath($composerLock)));
69 3
    }
70
}
71