1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Monitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\Backup\BackupDestination\BackupDestination; |
7
|
|
|
|
8
|
|
|
class BackupDestinationStatusFactory |
9
|
|
|
{ |
10
|
|
|
public static function createForMonitorConfig(array $monitorConfiguration): Collection |
11
|
|
|
{ |
12
|
|
|
return collect($monitorConfiguration)->flatMap(function (array $monitorProperties) { |
13
|
|
|
return BackupDestinationStatusFactory::createForSingleMonitor($monitorProperties); |
14
|
|
|
})->sortBy(function (BackupDestinationStatus $backupDestinationStatus) { |
15
|
|
|
return $backupDestinationStatus->backupDestination()->backupName().'-'. |
16
|
|
|
$backupDestinationStatus->backupDestination()->diskName(); |
17
|
|
|
}); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function createForSingleMonitor(array $monitorConfig): Collection |
21
|
|
|
{ |
22
|
|
|
return collect($monitorConfig['disks'])->map(function ($diskName) use ($monitorConfig) { |
23
|
|
|
$backupDestination = BackupDestination::create($diskName, $monitorConfig['name']); |
24
|
|
|
|
25
|
|
|
return new BackupDestinationStatus($backupDestination, static::buildHealthChecks($monitorConfig)); |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected static function buildHealthChecks($monitorConfig) |
30
|
|
|
{ |
31
|
|
|
return collect(array_get($monitorConfig, 'health_checks'))->map(function ($options, $class) { |
32
|
|
|
if (is_int($class)) { |
33
|
|
|
$class = $options; |
34
|
|
|
$options = []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return static::buildHealthCheck($class, $options); |
38
|
|
|
})->toArray(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected static function buildHealthCheck($class, $options) |
42
|
|
|
{ |
43
|
|
|
// A single value was passed - we'll instantiate it manually assuming it's the first argument |
44
|
|
|
if (! is_array($options)) { |
45
|
|
|
return new $class($options); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// A config array was given. Use reflection to match arguments |
49
|
|
|
return app()->makeWith($class, $options); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|