BaseAttachment::setBasicDetails()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Storage;
7
use Mostafaznv\Larupload\Actions\SetFileNameAction;
8
use Mostafaznv\Larupload\Enums\LaruploadFileType;
9
use Mostafaznv\Larupload\Enums\LaruploadMode;
10
use Mostafaznv\Larupload\Larupload;
11
12
trait BaseAttachment
13
{
14
    protected function setBasicDetails(): void
15
    {
16
        $fileName = SetFileNameAction::make($this->file, $this->namingMethod, $this->lang)->generate();
17
18
        $this->output['name'] = $fileName;
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->output['id'] = $this->id;
20
        $this->output['format'] = $this->file->getClientOriginalExtension();
21
        $this->output['size'] = $this->file->getSize();
22
        $this->output['type'] = $this->type->name;
23
        $this->output['mime_type'] = $this->file->getMimeType();
24
25
        if ($this->storeOriginalFileName) {
26
            $this->output['original_name'] = $this->file->getClientOriginalName();
27
        }
28
    }
29
30
    protected function setMediaDetails(): void
31
    {
32
        switch ($this->type) {
33
            case LaruploadFileType::VIDEO:
34
            case LaruploadFileType::AUDIO:
35
                $meta = $this->ffmpeg()->getMeta();
0 ignored issues
show
Bug introduced by
It seems like ffmpeg() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                $meta = $this->/** @scrutinizer ignore-call */ ffmpeg()->getMeta();
Loading history...
36
37
                $this->output['width'] = $meta->width;
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
                $this->output['height'] = $meta->height;
39
                $this->output['duration'] = $meta->duration;
40
41
                break;
42
43
            case LaruploadFileType::IMAGE:
44
                $meta = $this->img($this->file)->getMeta();
0 ignored issues
show
Bug introduced by
It seems like img() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
                $meta = $this->/** @scrutinizer ignore-call */ img($this->file)->getMeta();
Loading history...
45
46
                $this->output['width'] = $meta['width'];
47
                $this->output['height'] = $meta['height'];
48
                $this->output['dominant_color'] = $this->dominantColor
49
                    ? $this->img($this->file)->getDominantColor($this->file)
50
                    : null;
51
52
                break;
53
        }
54
    }
55
56
    /**
57
     * Set attributes before saving event
58
     */
59
    protected function setAttributes(Model $model): Model
60
    {
61
        if ($this->mode === LaruploadMode::HEAVY) {
62
            foreach ($this->output as $key => $value) {
63
                if ($key == 'original_name' and !$this->storeOriginalFileName) {
64
                    continue;
65
                }
66
67
                $model->{"{$this->name}_file_$key"} = $value;
68
            }
69
        }
70
        else {
71
            $model->{"{$this->name}_file_name"} = $this->output['name'] ?? null;
72
            $model->{"{$this->name}_file_meta"} = json_encode($this->output);
73
        }
74
75
        return $model;
76
    }
77
78
    /**
79
     * Upload original file
80
     */
81
    protected function uploadOriginalFile(string $id, ?string $disk = null): void
82
    {
83
        Storage::disk($disk ?: $this->disk)
84
            ->putFileAs(
85
                path: $this->getBasePath($id, Larupload::ORIGINAL_FOLDER),
0 ignored issues
show
Bug introduced by
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                path: $this->/** @scrutinizer ignore-call */ getBasePath($id, Larupload::ORIGINAL_FOLDER),
Loading history...
86
                file: $this->file,
87
                name: $this->output['name']
88
            );
89
    }
90
91
    /**
92
     * Clean directory before upload
93
     */
94
    protected function clean($id): void
95
    {
96
        $path = $this->getBasePath($id);
97
        Storage::disk($this->disk)->deleteDirectory($path);
98
99
        foreach ($this->output as $key => $value) {
100
            $this->output[$key] = null;
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
101
        }
102
    }
103
}
104