Completed
Pull Request — master (#12)
by
unknown
26:12 queued 11:12
created

SecuritySlackNotification   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A via() 0 3 1
C textFormatter() 0 28 7
A toSlack() 0 7 1
A toArray() 0 3 1
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())->markdown(['text']);
60
            });
61
    }
62
63
    /**
64
     * Get the array representation of the notification.
65
     *
66
     * @return array
67
     */
68
    public function toArray()
69
    {
70
        return $this->vulnerabilities;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    protected function textFormatter()
77
    {
78
        $count = count($this->vulnerabilities);
79
80
        $txt = sprintf("%d %s known vulnerabilities\n", $count, 1 === $count ? 'package has' : 'packages have');
81
82
        if (0 !== $count) {
83
            foreach ($this->vulnerabilities as $dependency => $issues) {
84
                $dependencyFullName = $dependency.' ('.$issues[ 'version' ].')';
85
                $txt .= "\n";
86
                $txt .= "*{$dependencyFullName}*"."\n".str_repeat('-', strlen($dependencyFullName))."\n";
87
88
                foreach ($issues[ 'advisories' ] as $issue => $details) {
89
                    $txt .= ' * ';
90
                    if ($details[ 'cve' ]) {
91
                        $txt .= "{$details[ 'cve' ]} ";
92
                    }
93
                    $txt .= "{$details[ 'title' ]} ";
94
95
                    if ('' !== $details[ 'link' ]) {
96
                        $txt .= "{$details[ 'link' ]}";
97
                    }
98
99
                    $txt .= "\n";
100
                }
101
            }
102
        }
103
        return $txt;
104
    }
105
}
106