Storage::call()   A
last analyzed

Complexity

Conditions 4
Paths 11

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 31
rs 9.6666
cc 4
nc 11
nop 0
1
<?php
2
namespace Health\Checks;
3
4
use Illuminate\Support\Facades\Storage as StorageFacade;
5
6
class Storage extends BaseCheck implements HealthCheckInterface
7
{
8
9
    /**
10
     *
11
     * {@inheritdoc}
12
     * @see \Health\Checks\HealthCheckInterface::call()
13
     */
14
    public function call()
15
    {
16
        $builder = $this->getBuilder();
17
18
        $name = $this->getParam('name');
19
        $file = $this->getParam('file', '/health.txt');
20
21
        $contents = $this->getParam('contents', random_bytes(32));
22
        $options = $this->getParam('options', []);
23
24
        try {
25
            StorageFacade::disk($name)->put($file, $contents, $options);
26
27
            $fileContents = StorageFacade::disk($name)->get($file);
28
29
            if ($contents !== $fileContents) {
30
                $builder->down()->withData('error', 'Contents mismatch');
31
            }
32
33
            if (! StorageFacade::disk($name)->delete($file)) {
34
                $builder->down()->withData('error', 'Could not delete file');
35
            }
36
        } catch (\Exception $exception) {
37
            $builder->down()->withData('error', $exception->getMessage());
38
        }
39
40
        $builder->withData('name', $name)
41
            ->withData('file', $file)
42
            ->withData('contents', $contents);
43
44
        return $builder->build();
45
    }
46
}
47