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

MaximumStorageInMegabytes::checkHealth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Tasks\Monitor\HealthChecks;
4
5
use Spatie\Backup\Helpers\Format;
6
use Spatie\Backup\Tasks\Monitor\HealthCheck;
7
use Spatie\Backup\BackupDestination\BackupDestination;
8
9
class MaximumStorageInMegabytes extends HealthCheck
10
{
11
    /** @var int */
12
    protected $maximumSizeInMegaBytes;
13
14
    public function __construct(int $maximumSizeInMegaBytes = 5000)
15
    {
16
        $this->maximumSizeInMegaBytes = $maximumSizeInMegaBytes;
17
    }
18
19
    public function checkHealth(BackupDestination $backupDestination)
20
    {
21
        $usageInBytes = $backupDestination->usedStorage();
22
23
        $this->failIf(
24
            $this->exceedsAllowance($usageInBytes),
25
            trans('backup::notifications.unhealthy_backup_found_full', [
26
                'disk_usage' => $this->humanReadableSize($usageInBytes),
27
                'disk_limit' => $this->humanReadableSize($this->bytes($this->maximumSizeInMegaBytes)),
28
            ])
29
        );
30
    }
31
32
    protected function exceedsAllowance(int $usageInBytes): bool
33
    {
34
        return $usageInBytes > $this->bytes($this->maximumSizeInMegaBytes);
35
    }
36
37
    protected function bytes(int $megaBytes): int
38
    {
39
        return $megaBytes * 1024 * 1024;
40
    }
41
42
    protected function humanReadableSize(int $sizeInBytes): string
43
    {
44
        return Format::humanReadableSize($sizeInBytes);
45
    }
46
}
47