Completed
Pull Request — master (#789)
by
unknown
02:03
created

UnhealthyBackupWasFound   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toMail() 0 21 2
A toSlack() 0 32 2
B problemDescription() 0 26 7
A inspectionFailure() 0 4 1
A setEvent() 0 6 1
1
<?php
2
3
namespace Spatie\Backup\Notifications\Notifications;
4
5
use Spatie\Backup\Exceptions\InvalidHealthCheck;
6
use Spatie\Backup\Notifications\BaseNotification;
7
use Illuminate\Notifications\Messages\MailMessage;
8
use Illuminate\Notifications\Messages\SlackMessage;
9
use Illuminate\Notifications\Messages\SlackAttachment;
10
use Spatie\Backup\Events\UnhealthyBackupWasFound as UnhealthyBackupWasFoundEvent;
11
12
class UnhealthyBackupWasFound extends BaseNotification
13
{
14
    /** @var \Spatie\Backup\Events\UnhealthyBackupWasFound */
15
    protected $event;
16
17
    public function toMail(): MailMessage
18
    {
19
        $mailMessage = (new MailMessage)
20
            ->error()
21
            ->subject(trans('backup::notifications.unhealthy_backup_found_subject', ['application_name' => $this->applicationName()]))
22
            ->line(trans('backup::notifications.unhealthy_backup_found_body', ['application_name' => $this->applicationName(), 'disk_name' => $this->diskName()]))
23
            ->line($this->problemDescription());
24
25
        $this->backupDestinationProperties()->each(function ($value, $name) use ($mailMessage) {
26
            $mailMessage->line("{$name}: $value");
27
        });
28
29
        if (optional($this->inspectionFailure())->wasUnexpected()) {
30
            $mailMessage
31
                ->line('Inspection: '.$this->inspectionFailure()->inspection()->name())
32
                ->line(trans('backup::notifications.exception_message', ['message' => $this->inspectionFailure()->reason()->getMessage()]))
33
                ->line(trans('backup::notifications.exception_trace', ['trace' => $this->inspectionFailure()->reason()->getTraceAsString()]));
34
        }
35
36
        return $mailMessage;
37
    }
38
39
    public function toSlack(): SlackMessage
40
    {
41
        $slackMessage = (new SlackMessage)
42
            ->error()
43
            ->from(config('backup.notifications.slack.username'), config('backup.notifications.slack.icon'))
44
            ->to(config('backup.notifications.slack.channel'))
45
            ->content(trans('backup::notifications.unhealthy_backup_found_subject_title', ['application_name' => $this->applicationName(), 'problem' => $this->problemDescription()]))
46
            ->attachment(function (SlackAttachment $attachment) {
47
                $attachment->fields($this->backupDestinationProperties()->toArray());
48
            });
49
50
        if (optional($this->inspectionFailure())->wasUnexpected()) {
51
            $slackMessage
52
                ->attachment(function (SlackAttachment $attachment) {
53
                    $attachment
54
                        ->title('Inspection')
55
                        ->content($this->inspectionFailure()->inspection()->name());
56
                })
57
                ->attachment(function (SlackAttachment $attachment) {
58
                    $attachment
59
                        ->title(trans('backup::notifications.exception_message_title'))
60
                        ->content($this->inspectionFailure()->reason()->getMessage());
61
                })
62
                ->attachment(function (SlackAttachment $attachment) {
63
                    $attachment
64
                        ->title(trans('backup::notifications.exception_trace_title'))
65
                        ->content($this->inspectionFailure()->reason()->getTraceAsString());
66
                });
67
        }
68
69
        return $slackMessage;
70
    }
71
72
    protected function problemDescription(): string
73
    {
74
        $backupStatus = $this->event->backupDestinationStatus;
75
76
        if (! $backupStatus->isReachable()) {
77
            return trans('backup::notification.unhealthy_backup_found_not_reachable', ['error' => $backupStatus->connectionError()]);
78
        }
79
80
        if ($backupStatus->amountOfBackups() === 0) {
81
            return trans('backup::notifications.unhealthy_backup_found_empty');
82
        }
83
84
        if ($backupStatus->usesTooMuchStorage()) {
85
            return trans('backup::notifications.unhealthy_backup_found_full', ['disk_usage' => $backupStatus->humanReadableUsedStorage(), 'disk_limit' => $backupStatus->humanReadableAllowedStorage()]);
86
        }
87
88
        if ($backupStatus->newestBackupIsTooOld()) {
89
            return trans('backup::notifications.unhealthy_backup_found_old', ['date' => $backupStatus->dateOfNewestBackup()->format('Y/m/d h:i:s')]);
90
        }
91
92
        if ($this->inspectionFailure() && ! $this->inspectionFailure()->wasUnexpected()) {
93
            return $this->inspectionFailure()->reason()->getMessage();
94
        }
95
96
        return trans('backup::notifications.unhealthy_backup_found_unknown');
97
    }
98
99
    protected function inspectionFailure()
100
    {
101
        return $this->event->backupDestinationStatus->getFailedInspection();
102
    }
103
104
    public function setEvent(UnhealthyBackupWasFoundEvent $event)
105
    {
106
        $this->event = $event;
107
108
        return $this;
109
    }
110
}
111