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

UnhealthyBackupWasFound::problemDescription()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 0
dl 0
loc 22
rs 8.6737
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\UnhealthyBackupWasFound as UnhealthyBackupWasFoundEvent;
9
use Spatie\Backup\Notifications\BaseNotification;
10
11
class UnhealthyBackupWasFound extends BaseNotification
12
{
13
    /** @var \Spatie\Backup\Events\UnhealthyBackupWasFound */
14
    protected $event;
15
16
    public function toMail(): MailMessage
17
    {
18
        $mailMessage = (new MailMessage)
19
            ->error()
20
            ->subject("Important: The backups for `{$this->applicationName()}` are unhealthy")
21
            ->line("The backups for `{$this->applicationName()}` on disk `{$this->diskName()}` are unhealthy.")
22
            ->line($this->problemDescription());
23
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("Important: The backups for `{$this->applicationName()}` are unhealthy. {$this->problemDescription()}")
37
            ->attachment(function (SlackAttachment $attachment) {
38
                $attachment->fields($this->backupDestinationProperties()->toArray());
39
            });
40
    }
41
42
    protected function problemDescription(): string
43
    {
44
        $backupStatus = $this->event->backupDestinationStatus;
45
46
        if (! $backupStatus->isReachable()) {
47
            return "The backup destination cannot be reached. {$backupStatus->connectionError()}";
48
        }
49
50
        if (! $backupStatus->amountOfBackups() === 0) {
51
            return 'There are no backups of this application at all.';
52
        }
53
54
        if ($backupStatus->usesTooMuchStorage()) {
55
            return "The backups are using too much storage. Current usage is {$backupStatus->humanReadableUsedStorage()} which is higher than the allowed limit of {$backupStatus->humanReadableAllowedStorage()}.";
56
        }
57
58
        if ($backupStatus->newestBackupIsTooOld()) {
59
            return "The latest backup made on {$backupStatus->dateOfNewestBackup()->format('Y/m/d h:i:s')} is considered too old.";
60
        }
61
62
        return 'Sorry, an exact reason cannot be determined.';
63
    }
64
65
    public function setEvent(UnhealthyBackupWasFoundEvent $event)
66
    {
67
        $this->event = $event;
68
69
        return $this;
70
    }
71
}
72