FileJsonField::fileDownload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace JsonFieldCast\Json;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Str;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
12
class FileJsonField extends AbstractMeta
13
{
14
    public static string $fieldName      = 'name';
15
    public static string $fieldDate      = 'date';
16
    public static string $fieldPath      = 'path';
17
    public static string $fieldDisk      = 'disk';
18
    public static string $fieldSize      = 'size';
19
    public static string $fieldExtension = 'extension';
20
21
    public static string $dateFormat = 'Y-m-d H:i:s';
22
23
    /**
24
     * Is old file should be deleted before store new.
25
     *
26
     * @var bool
27
     */
28
    protected bool $unique;
29
30 8
    public function __construct(Model $model, array $data = [], bool $unique = true)
31
    {
32 8
        parent::__construct($model, $data);
33 8
        $this->unique = $unique;
34
    }
35
36
    /**
37
     * @param  UploadedFile  $file
38
     *
39
     * @return string
40
     */
41 6
    protected function generateFileName(UploadedFile $file): string
42
    {
43 6
        $suffix = ".{$file->extension()}";
44
45 6
        return Str::limit(Str::slug(Str::beforeLast($file->getClientOriginalName(), $suffix)), 50, '').$suffix;
46
    }
47
48
    /**
49
     * @param  UploadedFile  $file
50
     * @param  string|null  $disk
51
     * @param  string  $path
52
     *
53
     * @return static
54
     */
55 6
    public function storeUploadedFile(UploadedFile $file, ?string $disk = null, string $path = ''): static
56
    {
57 6
        if ($this->unique) {
58 6
            $this->delete();
59
        }
60
61 6
        $filename = $this->generateFileName($file);
62
63 6
        $filePath = $file->storeAs(
64 6
            $path,
65 6
            $filename,
66 6
            ['disk' => $disk]
67 6
        );
68
69 6
        $this->setData([
70 6
            static::$fieldName      => $filename,
71 6
            static::$fieldDate      => Carbon::now()->format(static::$dateFormat),
72 6
            static::$fieldPath      => $filePath,
73 6
            static::$fieldDisk      => $disk,
74 6
            static::$fieldSize      => $file->getSize(),
75 6
            static::$fieldExtension => $file->extension(),
76 6
        ]);
77
78 6
        return $this;
79
    }
80
81
    /**
82
     * Unlink file and clear data.
83
     *
84
     * @return static
85
     */
86 6
    public function delete(): static
87
    {
88 6
        if ($this->exists()) {
89 2
            $path = $this->getAttribute(static::$fieldPath);
90 2
            Storage::disk($this->getAttribute(static::$fieldDisk))->delete($path);
91
        }
92
93 6
        $this->setData([]);
94
95 6
        return $this;
96
    }
97
98
    /**
99
     * Check is current file exists.
100
     *
101
     * @return bool
102
     */
103 8
    public function exists(): bool
104
    {
105 8
        $path = $this->getAttribute(static::$fieldPath);
106
107 8
        if ($path) {
108 6
            return Storage::disk($this->getAttribute(static::$fieldDisk))->exists($path);
109
        }
110
111 8
        return false;
112
    }
113
114
    /**
115
     * Get filename.
116
     *
117
     * @return string|null
118
     */
119 6
    public function fileName(): ?string
120
    {
121 6
        $fileName = $this->getAttribute(static::$fieldName);
122 6
        if (!is_string($fileName)) {
123 2
            return null;
124
        }
125
126 4
        return $fileName ?: null;
127
    }
128
129
    /**
130
     * Get absolute path to file
131
     *
132
     * @return string|null
133
     */
134 6
    public function filePath(): ?string
135
    {
136 6
        $path = $this->getAttribute(static::$fieldPath);
137
138 6
        if ($path) {
139 4
            return Storage::disk($this->getAttribute(static::$fieldDisk))->path($path);
140
        }
141
142 2
        return null;
143
    }
144
145
    /**
146
     * Get file url
147
     *
148
     * @return string|null
149
     */
150 6
    public function fileUrl(): ?string
151
    {
152 6
        $path = $this->getAttribute(static::$fieldPath);
153
154 6
        if ($path) {
155 4
            return Storage::disk($this->getAttribute(static::$fieldDisk))->url($path);
156
        }
157
158 2
        return null;
159
    }
160
161
    /**
162
     * Get file stream
163
     *
164
     * @return \Symfony\Component\HttpFoundation\StreamedResponse|null
165
     */
166 4
    public function fileDownload(): ?StreamedResponse
167
    {
168 4
        $path = $this->getAttribute(static::$fieldPath);
169
170 4
        if ($path) {
171 2
            return Storage::disk($this->getAttribute(static::$fieldDisk))->download($path);
172
        }
173
174 2
        return null;
175
    }
176
177 2
    public function fileInfo(): ?array
178
    {
179 2
        if (!$this->exists()) {
180
            return null;
181
        }
182
183 2
        return [
184 2
            static::$fieldName      => $this->getAttribute(static::$fieldName),
185 2
            static::$fieldDate      => $this->getAttribute(static::$fieldDate),
186 2
            static::$fieldSize      => $this->getAttribute(static::$fieldSize),
187 2
            static::$fieldExtension => $this->getAttribute(static::$fieldExtension),
188 2
            'url'                   => $this->fileUrl(),
189 2
        ];
190
    }
191
}
192