ExportStoredFile::newFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NovaResourceDynamicExport\Models;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
6
use Illuminate\Database\Eloquent\Factories\HasFactory;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Storage;
9
use JsonFieldCast\Casts\SimpleJsonField;
10
use NovaResourceDynamicExport\Database\Factories\ExportStoredFileFactory;
11
12
/**
13
 * @property string $type
14
 * @property string $disk
15
 * @property string $path
16
 * @property string $name
17
 * @property \JsonFieldCast\Json\SimpleJsonField $meta
18
 * @property-read string $download_link
19
 */
20
class ExportStoredFile extends Model
21
{
22
    use HasFactory;
23
24
    protected $guarded = [];
25
26
    protected $casts = [
27
        'meta' => SimpleJsonField::class,
28
    ];
29
30 9
    public function getTable(): string
31
    {
32 9
        return config('nova-resource-dynamic-export.tables.export_stored_files');
33
    }
34
35 9
    protected static function boot(): void
36
    {
37 9
        parent::boot();
38
39 9
        self::deleting(function (self $model) {
40 1
            $storage = Storage::disk($model->disk);
41 1
            if ($storage->exists($model->path)) {
42 1
                $storage->delete($model->path);
43
            }
44 9
        });
45
    }
46
47 5
    public static function init(string $type, string $disk, string $path, string $name, ?\Closure $tap = null): static
48
    {
49 5
        $model       = new static();
50 5
        $model->type = $type;
51 5
        $model->disk = $disk;
52 5
        $model->path = $path;
53 5
        $model->name = $name;
54
55 5
        if(is_callable($tap)) {
56 5
            $tap($model);
57
        }
58
59 5
        return $model;
60
    }
61
62 3
    public function downloadLink(): Attribute
63
    {
64 3
        return Attribute::get(fn () => route(config('nova-resource-dynamic-export.defaults.download_route'), $this->path));
65
    }
66
67 5
    protected static function newFactory(): ExportStoredFileFactory
68
    {
69 5
        return ExportStoredFileFactory::new();
70
    }
71
}
72