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

MaximumStorageInMegabytes   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkHealth() 0 12 1
A exceedsAllowance() 0 4 1
A bytes() 0 4 1
A humanReadableSize() 0 4 1
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