These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Backup\Notifications\Notifications; |
||
4 | |||
5 | use Spatie\Backup\Notifications\BaseNotification; |
||
6 | use Illuminate\Notifications\Messages\MailMessage; |
||
7 | use Illuminate\Notifications\Messages\SlackMessage; |
||
8 | use Illuminate\Notifications\Messages\SlackAttachment; |
||
9 | use Spatie\Backup\Events\CleanupHasFailed as CleanupHasFailedEvent; |
||
10 | |||
11 | View Code Duplication | class CleanupHasFailed extends BaseNotification |
|
0 ignored issues
–
show
|
|||
12 | { |
||
13 | /** @var \Spatie\Backup\Events\CleanupHasFailed */ |
||
14 | protected $event; |
||
15 | |||
16 | public function toMail(): MailMessage |
||
17 | { |
||
18 | $mailMessage = (new MailMessage) |
||
19 | ->error() |
||
20 | ->subject(trans('backup::notifications.cleanup_failed_subject', ['application_name' => $this->applicationName()])) |
||
21 | ->line(trans('backup::notifications.cleanup_failed_body', ['application_name' => $this->applicationName()])) |
||
22 | ->line(trans('backup::notifications.exception_message', ['message' => $this->event->exception->getMessage()])) |
||
23 | ->line(trans('backup::notifications.exception_trace', ['trace' => $this->event->exception->getTraceAsString()])); |
||
24 | |||
25 | $this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) { |
||
26 | $mailMessage->line("{$name}: $value"); |
||
27 | }); |
||
28 | |||
29 | return $mailMessage; |
||
30 | } |
||
31 | |||
32 | public function toSlack(): SlackMessage |
||
33 | { |
||
34 | return (new SlackMessage) |
||
35 | ->error() |
||
36 | ->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon')) |
||
37 | ->to(config('backup.notifications.slack.channel')) |
||
38 | ->content(trans('backup::notifications.cleanup_failed_subject', ['application_name' => $this->applicationName()])) |
||
39 | ->attachment(function (SlackAttachment $attachment) { |
||
40 | $attachment |
||
41 | ->title(trans('backup::notifications.exception_message_title')) |
||
42 | ->content($this->event->exception->getMessage()); |
||
43 | }) |
||
44 | ->attachment(function (SlackAttachment $attachment) { |
||
45 | $attachment |
||
46 | ->title(trans('backup::notifications.exception_message_trace')) |
||
47 | ->content($this->event->exception->getTraceAsString()); |
||
48 | }) |
||
49 | ->attachment(function (SlackAttachment $attachment) { |
||
50 | $attachment->fields($this->backupDestinationProperties()->toArray()); |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | public function setEvent(CleanupHasFailedEvent $event) |
||
55 | { |
||
56 | $this->event = $event; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | } |
||
61 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.