Completed
Push — master ( 326b3e...061c53 )
by Freek
09:13
created

CleanupHasFailed::toSlack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Notifications\Notifications;
4
5
use Illuminate\Notifications\Messages\MailMessage;
6
use Illuminate\Notifications\Messages\SlackAttachment;
7
use Illuminate\Notifications\Messages\SlackMessage;
8
use Spatie\Backup\Events\CleanupHasFailed as CleanupHasFailedEvent;
9
use Spatie\Backup\Notifications\BaseNotification;
10
11 View Code Duplication
class CleanupHasFailed extends BaseNotification
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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("Cleaning up the backups of `{$this->applicationName()}` failed.")
21
            ->line("An error occurred while cleaning up the backups of `{$this->applicationName()}`")
22
            ->line("Exception message: `{$this->event->exception->getMessage()}`")
23
            ->line("Exception 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
            ->content("An error occurred while cleaning up the backups of `{$this->applicationName()}`")
37
            ->attachment(function (SlackAttachment $attachment) {
38
                $attachment
39
                    ->title('Exception message')
40
                    ->content($this->event->exception->getMessage());
41
            })
42
            ->attachment(function (SlackAttachment $attachment) {
43
                $attachment
44
                    ->title('Exception trace')
45
                    ->content($this->event->exception->getTraceAsString());
46
            })
47
            ->attachment(function (SlackAttachment $attachment) {
48
                $attachment->fields($this->backupDestinationProperties()->toArray());
49
            });
50
    }
51
52
    public function setEvent(CleanupHasFailedEvent $event)
53
    {
54
        $this->event = $event;
55
56
        return $this;
57
    }
58
}
59