Completed
Push — master ( f62f21...f72c97 )
by Freek
05:09
created

BackupDestinationStatus   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 24
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 201
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setMaximumAgeOfNewestBackupInDays() 0 6 1
A getMaximumAgeOfNewestBackupInDays() 0 4 1
A setMaximumStorageUsageInMegabytes() 0 6 1
A getBackupName() 0 4 1
A getFilesystemName() 0 4 1
A getAmountOfBackups() 0 4 1
A getDateOfNewestBackup() 0 10 2
A newestBackupIsToolOld() 0 10 2
A getUsedStorage() 0 4 1
A getConnectionError() 0 4 1
A isReachable() 0 4 1
A getMaximumAllowedUsageInBytes() 0 4 1
A backupUsesTooMuchStorage() 0 10 2
A isHealthy() 0 16 4
A getHumanReadableAllowedStorage() 0 10 2
A getHumanReadableUsedStorage() 0 4 1
1
<?php
2
3
namespace Spatie\Backup\Tasks\Monitor;
4
5
use Carbon\Carbon;
6
use Spatie\Backup\BackupDestination\BackupDestination;
7
use Spatie\Backup\Helpers\Format;
8
9
class BackupDestinationStatus
10
{
11
    /** @var \Spatie\Backup\BackupDestination\BackupDestination */
12
    protected $backupDestination;
13
14
    /**  @var int */
15
    protected $maximumAgeOfNewestBackupInDays = 1;
16
17
    /**  @var int */
18
    protected $maximumStorageUsageInMegabytes = 5000;
19
20
    /** @var string */
21
    protected $filesystemName;
22
23
    /** @var bool */
24
    protected $reachable;
25
26
    /**
27
     * @param \Spatie\Backup\BackupDestination\BackupDestination $backupDestination
28
     * @param string                                             $filesystemName
29
     */
30
    public function __construct(BackupDestination $backupDestination, $filesystemName)
31
    {
32
        $this->backupDestination = $backupDestination;
33
        $this->filesystemName = $filesystemName;
34
35
        $this->reachable = $this->backupDestination->isReachable();
36
    }
37
38
    /**
39
     * @param int $days
40
     *
41
     * @return \Spatie\Backup\Tasks\Monitor\BackupDestinationStatus
42
     */
43
    public function setMaximumAgeOfNewestBackupInDays($days)
44
    {
45
        $this->maximumAgeOfNewestBackupInDays = $days;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getMaximumAgeOfNewestBackupInDays()
54
    {
55
        return $this->maximumAgeOfNewestBackupInDays;
56
    }
57
58
    /**
59
     * @param int $megabytes
60
     *
61
     * @return \Spatie\Backup\Tasks\Monitor\BackupDestinationStatus
62
     */
63
    public function setMaximumStorageUsageInMegabytes($megabytes)
64
    {
65
        $this->maximumStorageUsageInMegabytes = $megabytes;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getBackupName()
74
    {
75
        return $this->backupDestination->getBackupName();
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getFilesystemName()
82
    {
83
        return $this->filesystemName;
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getAmountOfBackups()
90
    {
91
        return $this->backupDestination->getBackups()->count();
92
    }
93
94
    /**
95
     * @return \Carbon\Carbon|null
96
     */
97
    public function getDateOfNewestBackup()
98
    {
99
        $newestBackup = $this->backupDestination->getNewestBackup();
100
101
        if (is_null($newestBackup)) {
102
            return;
103
        }
104
105
        return $newestBackup->date();
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    public function newestBackupIsToolOld()
112
    {
113
        if (!count($this->backupDestination->getBackups())) {
114
            return true;
115
        }
116
117
        $maximumDate = Carbon::now()->subDays($this->maximumAgeOfNewestBackupInDays);
118
119
        return !$this->backupDestination->isNewestBackupOlderThan($maximumDate);
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    public function getUsedStorage()
126
    {
127
        return $this->backupDestination->getUsedStorage();
128
    }
129
130
    /**
131
     * @return \Exception
132
     */
133
    public function getConnectionError()
134
    {
135
        return $this->backupDestination->getConnectionError();
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function isReachable()
142
    {
143
        return $this->reachable;
144
    }
145
146
    /**
147
     * @return int
148
     */
149
    public function getMaximumAllowedUsageInBytes()
150
    {
151
        return $this->maximumStorageUsageInMegabytes * 1024 * 1024;
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function backupUsesTooMuchStorage()
158
    {
159
        $maximumInBytes = $this->getMaximumAllowedUsageInBytes();
160
161
        if ($maximumInBytes === 0) {
162
            return false;
163
        }
164
165
        return $this->getUsedStorage() > $maximumInBytes;
166
    }
167
168
    /**
169
     * @return bool
170
     */
171
    public function isHealthy()
172
    {
173
        if (!$this->backupDestination->isReachable()) {
174
            return false;
175
        }
176
177
        if ($this->backupUsesTooMuchStorage()) {
178
            return false;
179
        }
180
181
        if ($this->newestBackupIsToolOld()) {
182
            return false;
183
        }
184
185
        return true;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getHumanReadableAllowedStorage()
192
    {
193
        $maximumInBytes = $this->getMaximumAllowedUsageInBytes();
194
195
        if ($maximumInBytes === 0) {
196
            return 'unlimited';
197
        }
198
199
        return Format::getHumanReadableSize($maximumInBytes);
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getHumanReadableUsedStorage()
206
    {
207
        return Format::getHumanReadableSize($this->getUsedStorage());
208
    }
209
}
210