Completed
Push — master ( 7b686b...b265e2 )
by Avtandil
09:58
created

AvailableDiskSpace::check()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 20
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
13
14
use BeyondCode\SelfDiagnosis\Checks\Check;
15
use Illuminate\Support\Arr;
16
use InvalidArgumentException;
17
use Longman\LaravelLodash\SelfDiagnosis\ParsesNumValues;
18
19
class AvailableDiskSpace implements Check
20
{
21
    use ParsesNumValues;
22
23
    /** @var array */
24
    private $options = [];
25
26
    /**
27
     * The name of the check.
28
     *
29
     * @param array $config
30
     * @return string
31
     */
32
    public function name(array $config): string
33
    {
34
        return trans('lodash::checks.available_disk_space.name');
35
    }
36
37
    public function check(array $config): bool
38
    {
39
        $paths = Arr::get($config, 'paths', []);
40
41
        foreach ($paths as $path => $value) {
42
            $actual_space = disk_free_space($path);
43
            if ($actual_space === false) {
44
                throw new InvalidArgumentException('Can not get free space amount for path: ' . $path);
45
            }
46
            $actual_space = (int) $actual_space;
47
            $bytes = $this->toBytes($value);
48
            if ($actual_space < $bytes) {
49
                $this->options[$path] = $actual_space;
50
            }
51
        }
52
53
        return count($this->options) === 0;
54
    }
55
56
    public function message(array $config): string
57
    {
58
        $options = [];
59
        foreach ($this->options as $option => $value) {
60
            $options[] = '"' . $option . '": free space: ' . $this->fromBytes($value);
61
        }
62
63
        return trans('lodash::checks.available_disk_space.message', [
64
            'options' => implode(PHP_EOL, $options),
65
        ]);
66
    }
67
}
68