Completed
Push — master ( 6ff2c5...81c1a7 )
by Avtandil
02:39
created

FilesystemsAreAvailable::check()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

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 19
ccs 0
cts 10
cp 0
crap 20
rs 9.6333
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\Filesystem\FilesystemManager;
16
use Throwable;
17
18
use function app;
19
use function implode;
20
use function now;
21
use function trans;
22
23
use const PHP_EOL;
24
25
class FilesystemsAreAvailable implements Check
26
{
27
    /** @var \Illuminate\Filesystem\FilesystemManager */
28
    private $filesystemManager;
29
30
    /** @var array */
31
    private $options = [];
32
33
    public function name(array $config): string
34
    {
35
        return trans('lodash::checks.filesystems_are_available.name');
36
    }
37
38
    public function check(array $config): bool
39
    {
40
        $this->filesystemManager = app(FilesystemManager::class);
41
42
        foreach ($config['disks'] as $disk) {
43
            try {
44
                $status = $this->checkDisk($disk);
45
            } catch (Throwable $e) {
46
                $this->options[$disk] = $e->getMessage();
47
                continue;
48
            }
49
50
            if (! $status) {
51
                $this->options[$disk] = false;
52
            }
53
        }
54
55
        return count($this->options) === 0;
56
    }
57
58
    private function checkDisk(string $disk): bool
59
    {
60
        /** @var \Illuminate\Filesystem\FilesystemAdapter $diskInstance */
61
        $diskInstance = $this->filesystemManager->disk($disk);
62
63
        $file = 'test/test.txt';
64
65
        $status = $diskInstance->put($file, now()->toString());
66
        if (! $status) {
67
            return false;
68
        }
69
70
        $status = $diskInstance->delete($file);
71
        if (! $status) {
72
            return false;
73
        }
74
75
        return $status;
76
    }
77
78
    public function message(array $config): string
79
    {
80
        $options = [];
81
        foreach ($this->options as $option => $value) {
82
            $message = 'Disk "' . $option . '" is not available';
83
            if (! empty($value)) {
84
                $message .= '. Reason: ' . $value;
85
            }
86
87
            $options[] = $message;
88
        }
89
90
        return trans('lodash::checks.filesystems_are_available.message', [
91
            'options' => implode(PHP_EOL, $options),
92
        ]);
93
    }
94
}
95