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

SecuritySlackNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jorijn\LaravelSecurityChecker\Notifications;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Queue\SerializesModels;
7
use Illuminate\Notifications\Notification;
8
use Illuminate\Notifications\Messages\SlackMessage;
9
10
class SecuritySlackNotification extends Notification
11
{
12
    use SerializesModels, Queueable;
13
14
    /**
15
     *
16
     * @var array
17
     */
18
    protected $vulnerabilities;
19
20
    /**
21
     *
22
     * @var string
23
     */
24
    protected $composerLockPath;
25
26
    /**
27
     * Create a new notification instance.
28
     *
29
     * @param $vulnerabilities
30
     * @param $composerLockPath
31
     */
32
    public function __construct($vulnerabilities, $composerLockPath)
33
    {
34
        $this->vulnerabilities = $vulnerabilities;
35
        $this->composerLockPath = $composerLockPath;
36
    }
37
38
    /**
39
     * Get the notification's delivery channels.
40
     *
41
     * @return array
42
     */
43
    public function via()
44
    {
45
        return ['slack'];
46
    }
47
48
    /**
49
     * Get the slack representation of the notification.
50
     *
51
     * @return SlackMessage
52
     */
53
    public function toSlack()
54
    {
55
        return (new SlackMessage)
56
            ->from(config('app.url'))
57
            ->content("*Security Check Report:* `{$this->composerLockPath}`")
58
            ->attachment(function ($attachment) {
59
                $attachment->content($this->textFormatter())
60
                    ->markdown(['text']);
61
            });
62
    }
63
64
    /**
65
     * Get the array representation of the notification.
66
     *
67
     * @return array
68
     */
69
    public function toArray()
70
    {
71
        return $this->vulnerabilities;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function textFormatter()
78
    {
79
        $count = count($this->vulnerabilities);
80
81
        $txt = sprintf("%d %s known vulnerabilities\n", $count, 1 === $count ? 'package has' : 'packages have');
82
83
        if (0 !== $count) {
84
            foreach ($this->vulnerabilities as $dependency => $issues) {
85
                $dependencyFullName = $dependency . ' (' . $issues['version'] . ')';
86
                $txt .= "\n";
87
                $txt .= "*{$dependencyFullName}*" . "\n" . str_repeat('-', strlen($dependencyFullName)) . "\n";
88
89
                foreach ($issues['advisories'] as $issue => $details) {
90
                    $txt .= ' * ';
91
                    if ($details['cve']) {
92
                        $txt .= "{$details['cve']} ";
93
                    }
94
                    $txt .= "{$details['title']} ";
95
96
                    if ('' !== $details['link']) {
97
                        $txt .= "{$details['link']}";
98
                    }
99
100
                    $txt .= "\n";
101
                }
102
            }
103
        }
104
        return $txt;
105
    }
106
}
107