Completed
Push — master ( 7ddb25...935f58 )
by Freek
03:09
created

Notifier::unhealthyBackupWasFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Backup\Notifications;
4
5
use Spatie\Backup\BackupDestination\BackupDestination;
6
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatus;
7
use Exception;
8
9
class Notifier
10
{
11
    /** @var array */
12
    protected $config;
13
14
    public function __construct()
15
    {
16
        $this->subject = config('laravel-backup.backup.name').' backups';
0 ignored issues
show
Bug introduced by
The property subject does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
    }
18
19
    public function backupWasSuccessful()
20
    {
21
        $this->sendNotification(
22
            'whenBackupWasSuccessful',
23
            $this->subject,
24
            'Successfully took a new backup',
25
            BaseSender::TYPE_SUCCESS
26
        );
27
    }
28
29
    public function backupHasFailed(Exception $exception, BackupDestination $backupDestination = null)
30
    {
31
        $extraMessage = $backupDestination ? "to {$backupDestination->getFilesystemType()}-filesystem" : '';
32
33
        $this->sendNotification(
34
            'whenBackupHasFailed',
35
            "{$this->subject} : error",
36
            "Failed to backup {$extraMessage} because: {$exception->getMessage()}",
37
            BaseSender::TYPE_ERROR
38
        );
39
    }
40
41
    public function cleanupWasSuccessFul(BackupDestination $backupDestination)
42
    {
43
        $this->sendNotification(
44
            'whenCleanupWasSuccessful',
45
            $this->subject,
46
            "Successfully cleaned up the backups on {$backupDestination->getFilesystemType()}-filesystem",
47
            BaseSender::TYPE_SUCCESS
48
        );
49
    }
50
51
    public function cleanupHasFailed(Exception $exception)
52
    {
53
        $this->sendNotification(
54
            'whenCleanupHasFailed',
55
            "{$this->subject} : error",
56
            "Failed to cleanup the backup because: {$exception->getMessage()}",
57
            BaseSender::TYPE_ERROR
58
        );
59
    }
60
61
    public function healthyBackupWasFound(BackupDestinationStatus $backupDestinationStatus)
62
    {
63
        $this->sendNotification(
64
            'whenHealthyBackupWasFound',
65
            "Healthy backup found for {$backupDestinationStatus->getBackupName()} on {$backupDestinationStatus->getFilesystemName()}-filesystem",
0 ignored issues
show
Deprecated Code introduced by
The method Spatie\Backup\Tasks\Moni...us::getFilesystemName() has been deprecated.

This method has been deprecated.

Loading history...
66
            "Backups on filesystem {$backupDestinationStatus->getFilesystemName()} are ok",
0 ignored issues
show
Deprecated Code introduced by
The method Spatie\Backup\Tasks\Moni...us::getFilesystemName() has been deprecated.

This method has been deprecated.

Loading history...
67
            BaseSender::TYPE_SUCCESS
68
        );
69
    }
70
71
    /**
72
     * @param \Spatie\Backup\Tasks\Monitor\BackupDestinationStatus $backupDestinationStatus
73
     */
74
    public function unhealthyBackupWasFound(BackupDestinationStatus $backupDestinationStatus)
75
    {
76
        $this->sendNotification(
77
            'whenUnhealthyBackupWasFound',
78
            "Unhealthy backup found for {$backupDestinationStatus->getBackupName()} on {$backupDestinationStatus->getFilesystemName()}-filesystem",
0 ignored issues
show
Deprecated Code introduced by
The method Spatie\Backup\Tasks\Moni...us::getFilesystemName() has been deprecated.

This method has been deprecated.

Loading history...
79
            UnhealthyBackupMessage::createForBackupDestinationStatus($backupDestinationStatus),
80
            BaseSender::TYPE_ERROR
81
        );
82
    }
83
84
    /**
85
     * @param string $eventName
86
     * @param string $subject
87
     * @param string $message
88
     * @param string $type
89
     */
90
    protected function sendNotification($eventName, $subject, $message, $type)
91
    {
92
        $senderNames = config("laravel-backup.notifications.events.{$eventName}");
93
94
        collect($senderNames)
95
            ->map(function ($senderName) {
96
                $className = $senderName;
97
98
                if (file_exists(__DIR__.'/Senders/'.ucfirst($senderName).'.php')) {
99
                    $className = '\\Spatie\\Backup\\Notifications\\Senders\\'.ucfirst($senderName);
100
                }
101
102
                return app($className);
103
            })
104
            ->each(function (SendsNotifications $sender) use ($subject, $message, $type) {
105
                $sender
106
                    ->setSubject($subject)
107
                    ->setMessage($message)
108
                    ->setType($type)
109
                    ->send();
110
            });
111
    }
112
}
113