AvailableDiskSpace   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A check() 0 20 4
A message() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
6
7
use BeyondCode\SelfDiagnosis\Checks\Check;
8
use Illuminate\Support\Arr;
9
use InvalidArgumentException;
10
use Longman\LaravelLodash\SelfDiagnosis\ParsesNumValues;
11
12
use function count;
13
use function disk_free_space;
14
use function implode;
15
16
use const PHP_EOL;
17
18
class AvailableDiskSpace implements Check
19
{
20
    use ParsesNumValues;
21
22
    /** @var array */
23
    private $options = [];
24
25
    /**
26
     * The name of the check.
27
     *
28
     * @param array $config
29
     * @return string
30
     */
31
    public function name(array $config): string
32
    {
33
        return trans('lodash::checks.available_disk_space.name');
34
    }
35
36
    public function check(array $config): bool
37
    {
38
        $paths = Arr::get($config, 'paths', []);
39
40
        foreach ($paths as $path => $value) {
41
            $actualSpace = disk_free_space($path);
42
            if ($actualSpace === false) {
43
                throw new InvalidArgumentException('Can not get free space amount for path: ' . $path);
44
            }
45
            $actualSpace = (int) $actualSpace;
46
            $bytes = $this->toBytes($value);
47
            if ($actualSpace >= $bytes) {
48
                continue;
49
            }
50
51
            $this->options[$path] = $actualSpace;
52
        }
53
54
        return count($this->options) === 0;
55
    }
56
57
    public function message(array $config): string
58
    {
59
        $options = [];
60
        foreach ($this->options as $option => $value) {
61
            $options[] = '"' . $option . '": free space: ' . $this->fromBytes($value);
62
        }
63
64
        return trans('lodash::checks.available_disk_space.message', [
65
            'options' => implode(PHP_EOL, $options),
66
        ]);
67
    }
68
}
69