MonitorCommand::handle()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Commands;
4
5
use Spatie\Backup\Events\HealthyBackupWasFound;
6
use Spatie\Backup\Events\UnhealthyBackupWasFound;
7
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatusFactory;
8
9
class MonitorCommand extends BaseCommand
10
{
11
    /** @var string */
12
    protected $signature = 'backup:monitor';
13
14
    /** @var string */
15
    protected $description = 'Monitor the health of all backups.';
16
17
    public function handle()
18
    {
19
        $hasError = false;
20
21
        $statuses = BackupDestinationStatusFactory::createForMonitorConfig(config('backup.monitor_backups'));
22
23
        foreach ($statuses as $backupDestinationStatus) {
24
            $diskName = $backupDestinationStatus->backupDestination()->diskName();
25
26
            if ($backupDestinationStatus->isHealthy()) {
27
                $this->info("The backups on {$diskName} are considered healthy.");
28
                event(new HealthyBackupWasFound($backupDestinationStatus));
29
            } else {
30
                $hasError = true;
31
                $this->error("The backups on {$diskName} are considered unhealthy!");
32
                event(new UnHealthyBackupWasFound($backupDestinationStatus));
33
            }
34
        }
35
36
        if ($hasError) {
37
            return 1;
38
        }
39
    }
40
}
41