| 1 | <?php |
||
| 2 | |||
| 3 | namespace Jorijn\LaravelSecurityChecker\Notifications; |
||
| 4 | |||
| 5 | use Illuminate\Bus\Queueable; |
||
| 6 | use Illuminate\Notifications\Messages\SlackMessage; |
||
| 7 | use Illuminate\Notifications\Notification; |
||
| 8 | use Illuminate\Queue\SerializesModels; |
||
| 9 | |||
| 10 | class SecuritySlackNotification extends Notification |
||
| 11 | { |
||
| 12 | use SerializesModels, Queueable; |
||
|
0 ignored issues
–
show
introduced
by
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 | $packageCount = \count($this->vulnerabilities); |
|
| 79 | 3 | $content = trans_choice('laravel-security-checker::messages.subject_new_vulnerabilities', $packageCount, [ |
|
| 80 | 3 | 'count' => $packageCount, |
|
| 81 | ]); |
||
| 82 | |||
| 83 | 3 | if ($packageCount > 0) { |
|
| 84 | 3 | foreach ($this->vulnerabilities as $dependency => $issues) { |
|
| 85 | 3 | $dependencyFullName = sprintf('%s (%s)', $dependency, $issues[ 'version' ]); |
|
| 86 | |||
| 87 | 3 | $content .= PHP_EOL; |
|
| 88 | 3 | $content .= sprintf('*%s*', $dependencyFullName); |
|
| 89 | 3 | $content .= PHP_EOL; |
|
| 90 | 3 | $content .= str_repeat('-', \strlen($dependencyFullName)); |
|
| 91 | 3 | $content .= PHP_EOL; |
|
| 92 | |||
| 93 | 3 | foreach ($issues[ 'advisories' ] as $issue => $details) { |
|
| 94 | 3 | $content .= ' * '; |
|
| 95 | |||
| 96 | 3 | if ($details[ 'cve' ]) { |
|
| 97 | 3 | $content .= $details[ 'cve' ].' '; |
|
| 98 | } |
||
| 99 | |||
| 100 | 3 | $content .= $details[ 'title' ].' '; |
|
| 101 | |||
| 102 | 3 | if (!empty($details[ 'link' ])) { |
|
| 103 | 3 | $content .= $details[ 'link' ]; |
|
| 104 | } |
||
| 105 | |||
| 106 | 3 | $content .= PHP_EOL; |
|
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | 3 | return $content; |
|
| 112 | } |
||
| 113 | } |
||
| 114 |