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