Passed
Pull Request — master (#12)
by
unknown
15:05
created

SecuritySlackCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 4
A __construct() 0 5 1
1
<?php
2
3
namespace Jorijn\LaravelSecurityChecker\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Notification;
7
use Jorijn\LaravelSecurityChecker\Notifications\SecuritySlackNotification;
8
use SensioLabs\Security\SecurityChecker;
9
10
class SecuritySlackCommand extends Command
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $name = 'security-check:slack';
16
17
    /**
18
     * @var string
19
     */
20
    protected $description = 'Send any vulnerabilities for packages you have in your composer.lock file to a Slack channel.';
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
     * Execute the command
41
     */
42
    public function handle()
43
    {
44
        // require that the user specifies a slack channel in the .env file
45
        if (!config('laravel-security-checker.slack_webhook_url')) {
46
            throw new \Exception('No Slack Webhook has been specified.');
47
        }
48
49
        // get the path to composer.lock
50
        $composerLock = base_path('composer.lock');
51
52
        // and feed it into the SecurityChecker
53
        $vulnerabilities = $this->checker->check($composerLock);
54
55
        // cancel execution here if user does not want to be notified when there are 0 vulns.
56
        $proceed = config('laravel-security-checker.email_even_without_vulnerabilities', false);
57
        if (count($vulnerabilities) === 0 && $proceed !== true) {
58
            return 0;
59
        }
60
61
        Notification::route('slack', config('laravel-security-checker.slack_webhook_url', null))
62
            ->notify(new SecuritySlackNotification($vulnerabilities, realpath($composerLock)));
63
    }
64
}
65