Completed
Push — master ( 9f6466...621974 )
by Freek
02:29 queued 58s
created

BackupDestinationStatus::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Tasks\Monitor;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Spatie\Backup\BackupDestination\BackupDestination;
8
use Spatie\Backup\Tasks\Monitor\HealthChecks\IsReachable;
9
10
class BackupDestinationStatus
11
{
12
    /** @var \Spatie\Backup\BackupDestination\BackupDestination */
13
    protected $backupDestination;
14
15
    /** @var array */
16
    protected $healthChecks;
17
18
    /** @var HealthCheckFailure|null */
19
    protected $healthCheckFailure;
20
21
    public function __construct(BackupDestination $backupDestination, array $healthChecks = [])
22
    {
23
        $this->backupDestination = $backupDestination;
24
25
        $this->healthChecks = $healthChecks;
26
    }
27
28
    public function backupDestination(): BackupDestination
29
    {
30
        return $this->backupDestination;
31
    }
32
33
    public function check(HealthCheck $check)
34
    {
35
        try {
36
            $check->checkHealth($this->backupDestination());
37
        } catch (Exception $exception) {
38
            return new HealthCheckFailure($check, $exception);
39
        }
40
41
        return true;
42
    }
43
44
    public function getHealthChecks(): Collection
45
    {
46
        return collect($this->healthChecks)->prepend(new IsReachable());
47
    }
48
49
    public function getHealthCheckFailure(): ?HealthCheckFailure
50
    {
51
        return $this->healthCheckFailure;
52
    }
53
54
    public function isHealthy(): bool
55
    {
56
        $healthChecks = $this->getHealthChecks();
57
58
        foreach ($healthChecks as $healthCheck) {
59
            $checkResult = $this->check($healthCheck);
60
61
            if ($checkResult instanceof HealthCheckFailure) {
62
                $this->healthCheckFailure = $checkResult;
63
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
}
71