Completed
Pull Request — master (#15)
by Yaro
08:09 queued 01:44
created

File::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Storage as IlluminateStorage;
9
use Yaro\Jarboe\Table\Fields\Adapters\RepeaterFile;
10
use Yaro\Jarboe\Table\Fields\Traits\Filename;
11
use Yaro\Jarboe\Table\Fields\Traits\Nullable;
12
use Yaro\Jarboe\Table\Fields\Traits\Placeholder;
13
use Yaro\Jarboe\Table\Fields\Traits\Storage;
14
15
class File extends AbstractField
16 45
{
17
    use Storage;
18 45
    use Nullable;
19 45
    use Placeholder;
20
    use Filename;
21 2
22
    public static $repeaterAdapterClass = RepeaterFile::class;
23 2
24
    public function __construct()
25 2
    {
26
        $this->disk = config('filesystems.default');
27
    }
28
29
    public function shouldSkip(Request $request)
30
    {
31
        $files = $request->file($this->name());
32
33
        return !$files;
34
    }
35
36
    public function value(Request $request)
37
    {
38
        $data = $request->get($this->name());
39
        $paths = Arr::get($data, 'paths', []);
40
        $files = Arr::get($data, 'files', []);
41
42
        if (!$this->isMultiple()) {
43
            $files = [$files];
44
        }
45
        $files = array_filter($files);
46
47
        /** @var UploadedFile $file */
48
        foreach ($files as $file) {
49
            $filename = $this->generateFilename($request, $file);
50 1
            $paths[] = $this->storeFile($file, $filename, $request);
51
        }
52 1
53 1
        if ($this->isMultiple()) {
54 1
            return $paths;
55
        }
56
        return array_filter($paths) ? array_pop($paths) : null;
57
    }
58 1
59
    public function getListView($model)
60 1
    {
61
        return view('jarboe::crud.fields.file.list', [
62 1
            'model' => $model,
63 1
            'field' => $this,
64 1
        ]);
65
    }
66
67
    public function getEditFormView($model)
68 1
    {
69
        $template = $this->isReadonly() ? 'readonly' : 'edit';
70 1
71 1
        return view('jarboe::crud.fields.file.'. $template, [
72
            'model' => $model,
73
            'field' => $this,
74
        ]);
75
    }
76
77
    public function getCreateFormView()
78
    {
79
        return view('jarboe::crud.fields.file.create', [
80
            'field' => $this,
81
        ]);
82
    }
83
84 View Code Duplication
    public function getPaths($model): array
85
    {
86
        if (is_null($model)) {
87
            return [];
88
        }
89
90
        $filepath = $this->getAttribute($model);
91
        if (!$filepath) {
92
            return [];
93
        }
94
95
        return is_array($filepath) ? $filepath : [$filepath];
96
    }
97
98
    public function formUrl(string $path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
99
    {
100
        $disk = IlluminateStorage::disk($this->getDisk());
101
102
        return $disk->url($path);
103
    }
104
105
    /**
106
     * @param Request $request
107
     * @param UploadedFile $file
108
     * @return string
109
     */
110
    private function generateFilename(Request $request, UploadedFile $file): string
111
    {
112
        $filename = $file->hashName();
113
114
        $closure = $this->filenameClosure;
115
        if (is_callable($closure)) {
116
            $filename = $closure($file, $request);
117
        }
118
119
        return (string) $filename;
120
    }
121
}
122