Completed
Pull Request — master (#12)
by
unknown
14:44
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
     * @return void
30
     */
31
    public function __construct($vulnerabilities, $composerLockPath)
32
    {
33
        $this->vulnerabilities = $vulnerabilities;
34
        $this->composerLockPath = $composerLockPath;
35
    }
36
37
    /**
38
     * Get the notification's delivery channels.
39
     *
40
     * @param  mixed  $notifiable
41
     * @return array
42
     */
43
    public function via($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function via(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        return ['slack'];
46
    }
47
48
    /**
49
     * Get the slack representation of the notification.
50
     *
51
     * @param  mixed  $notifiable
52
     * @return \Illuminate\Notifications\Messages\SlackMessage
53
     */
54
    public function toSlack($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function toSlack(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $vulnerabilities = $this->vulnerabilities;
0 ignored issues
show
Unused Code introduced by
The assignment to $vulnerabilities is dead and can be removed.
Loading history...
57
58
        return (new SlackMessage)
59
            ->from(config('app.url'))
60
            ->content("*Security Check Report:* `{$this->composerLockPath}`")
61
            ->attachment(function ($attachment) {
62
                $attachment->content($this->textFormatter())
63
                ->markdown(['pretext']);
64
            });
65
    }
66
67
    /**
68
     * Get the array representation of the notification.
69
     *
70
     * @param  mixed  $notifiable
71
     * @return array
72
     */
73
    public function toArray($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    public function toArray(/** @scrutinizer ignore-unused */ $notifiable)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return $this->vulnerabilities;
76
    }
77
78
    protected function textFormatter()
79
    {
80
        $count = count($this->vulnerabilities);
81
82
        $txt = sprintf("%d %s known vulnerabilities\n", $count, 1 === $count ? 'package has' : 'packages have');
83
84
        if (0 !== $count) {
85
            foreach ($this->vulnerabilities as $dependency => $issues) {
86
                $dependencyFullName = $dependency . ' (' . $issues['version'] . ')';
87
                $txt .= "\n";
88
                $txt .= "*{$dependencyFullName}*" . "\n" . str_repeat('-', strlen($dependencyFullName)) . "\n";
89
90
                foreach ($issues['advisories'] as $issue => $details) {
91
                    $txt .= ' * ';
92
                    if ($details['cve']) {
93
                        $txt .= "{$details['cve']} ";
94
                    }
95
                    $txt .= "{$details['title']} ";
96
97
                    if ('' !== $details['link']) {
98
                        $txt .= "{$details['link']}";
99
                    }
100
101
                    $txt .= "\n";
102
                }
103
            }
104
        }
105
        return $txt;
106
    }
107
}
108