Completed
Pull Request — master (#589)
by
unknown
01:50
created

BaseNotification   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A via() 0 6 1
A applicationName() 0 4 1
B backupDestinationProperties() 0 22 5
A backupDestination() 0 10 3
A applicationEnv() 0 4 2
A backupName() 0 4 1
A diskName() 0 4 1
1
<?php
2
3
namespace Spatie\Backup\Notifications;
4
5
use Spatie\Backup\Helpers\Format;
6
use Illuminate\Support\Collection;
7
use Illuminate\Notifications\Notification;
8
9
abstract class BaseNotification extends Notification
10
{
11
    /**
12
     * Get the notification's delivery channels.
13
     *
14
     * @return array
15
     */
16
    public function via()
17
    {
18
        $notificationChannels = config('backup.notifications.notifications.'.static::class);
19
20
        return array_filter($notificationChannels);
21
    }
22
23
    public function applicationName(): string
24
    {
25
        return config('app.name') ?? config('app.url') ?? 'Laravel application';
26
    }
27
    
28
    public function applicationEnv(): string
29
    {
30
        return config('app.env') ? ' (' . config('app.env') . ')' : '';
31
    }
32
33
    public function backupName(): string
34
    {
35
        return $this->backupDestination()->backupName();
36
    }
37
38
    public function diskName(): string
39
    {
40
        return $this->backupDestination()->diskName();
41
    }
42
43
    protected function backupDestinationProperties(): Collection
44
    {
45
        $backupDestination = $this->backupDestination();
46
47
        if (! $backupDestination) {
48
            return collect();
49
        }
50
51
        $newestBackup = $backupDestination->newestBackup();
52
        $oldestBackup = $backupDestination->oldestBackup();
53
54
        return collect([
55
            'Application name' => $this->applicationName() . $this->applicationEnv(),
56
            'Backup name' => $this->backupName(),
57
            'Disk' => $backupDestination->diskName(),
58
            'Newest backup size' => $newestBackup ? Format::humanReadableSize($newestBackup->size()) : 'No backups were made yet',
59
            'Amount of backups' => (string) $backupDestination->backups()->count(),
60
            'Total storage used' => Format::humanReadableSize($backupDestination->backups()->size()),
61
            'Newest backup date' => $newestBackup ? $newestBackup->date()->format('Y/m/d H:i:s') : 'No backups were made yet',
62
            'Oldest backup date' => $oldestBackup ? $oldestBackup->date()->format('Y/m/d H:i:s') : 'No backups were made yet',
63
        ])->filter();
64
    }
65
66
    /**
67
     * @return \Spatie\Backup\BackupDestination\BackupDestination|null
68
     */
69
    public function backupDestination()
70
    {
71
        if (isset($this->event->backupDestination)) {
72
            return $this->event->backupDestination;
0 ignored issues
show
Bug introduced by
The property event 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...
73
        }
74
75
        if (isset($this->event->backupDestinationStatus)) {
76
            return $this->event->backupDestinationStatus->backupDestination();
77
        }
78
    }
79
}
80