Passed
Branch develop (ae3b85)
by Jorijn
04:14
created

SecuritySlackNotification::textFormatter()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 2
nop 0
dl 0
loc 28
ccs 17
cts 17
cp 1
crap 7
rs 8.8333
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;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Queue\SerializesModels requires the property $class which is not provided by Jorijn\LaravelSecurityCh...curitySlackNotification.
Loading history...
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 6
    public function __construct($vulnerabilities, $composerLockPath)
33
    {
34 6
        $this->vulnerabilities = $vulnerabilities;
35 6
        $this->composerLockPath = $composerLockPath;
36 6
    }
37
38
    /**
39
     * Get the notification's delivery channels.
40
     *
41
     * @return array
42
     */
43 6
    public function via()
44
    {
45 6
        return [ 'slack' ];
46
    }
47
48
    /**
49
     * Get the slack representation of the notification.
50
     *
51
     * @return SlackMessage
52
     */
53 3
    public function toSlack()
54
    {
55 3
        return (new SlackMessage)
56 3
            ->from(config('app.url'))
57 3
            ->content("*Security Check Report:* `{$this->composerLockPath}`")
58 3
            ->attachment(function($attachment) {
59 3
                $attachment->content($this->textFormatter())->markdown(['text']);
60 3
            });
61
    }
62
63
    /**
64
     * Get the array representation of the notification.
65
     *
66
     * @return array
67
     */
68 3
    public function toArray()
69
    {
70 3
        return $this->vulnerabilities;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 3
    protected function textFormatter()
77
    {
78 3
        $count = count($this->vulnerabilities);
79
80 3
        $txt = sprintf("%d %s known vulnerabilities\n", $count, 1 === $count ? 'package has' : 'packages have');
81
82 3
        if (0 !== $count) {
83 3
            foreach ($this->vulnerabilities as $dependency => $issues) {
84 3
                $dependencyFullName = $dependency.' ('.$issues[ 'version' ].')';
85 3
                $txt .= "\n";
86 3
                $txt .= "*{$dependencyFullName}*"."\n".str_repeat('-', strlen($dependencyFullName))."\n";
87
88 3
                foreach ($issues[ 'advisories' ] as $issue => $details) {
89 3
                    $txt .= ' * ';
90 3
                    if ($details[ 'cve' ]) {
91 3
                        $txt .= "{$details[ 'cve' ]} ";
92
                    }
93 3
                    $txt .= "{$details[ 'title' ]} ";
94
95 3
                    if ('' !== $details[ 'link' ]) {
96 3
                        $txt .= "{$details[ 'link' ]}";
97
                    }
98
99 3
                    $txt .= "\n";
100
                }
101
            }
102
        }
103 3
        return $txt;
104
    }
105
}
106