Test Failed
Branch master (df1c42)
by Mostafa
15:23
created

BaseAttachment::setBasicDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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
26
    protected function setMediaDetails(): void
27
    {
28
        switch ($this->type) {
29
            case LaruploadFileType::VIDEO:
30
            case LaruploadFileType::AUDIO:
31
                $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

31
                $meta = $this->/** @scrutinizer ignore-call */ ffmpeg()->getMeta();
Loading history...
32
33
                $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...
34
                $this->output['height'] = $meta->height;
35
                $this->output['duration'] = $meta->duration;
36
37
                break;
38
39
            case LaruploadFileType::IMAGE:
40
                $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

40
                $meta = $this->/** @scrutinizer ignore-call */ img($this->file)->getMeta();
Loading history...
41
42
                $this->output['width'] = $meta['width'];
43
                $this->output['height'] = $meta['height'];
44
                $this->output['dominant_color'] = $this->dominantColor
45
                    ? $this->img($this->file)->getDominantColor($this->file)
46
                    : null;
47
48
                break;
49
        }
50
    }
51
52
    /**
53
     * Set attributes before saving event
54
     */
55
    protected function setAttributes(Model $model): Model
56
    {
57
        if ($this->mode === LaruploadMode::HEAVY) {
58
            foreach ($this->output as $key => $value) {
59
                $model->{"{$this->name}_file_$key"} = $value;
60
            }
61
        }
62
        else {
63
            $model->{"{$this->name}_file_name"} = $this->output['name'] ?? null;
64
            $model->{"{$this->name}_file_meta"} = json_encode($this->output);
65
        }
66
67
        return $model;
68
    }
69
70
    /**
71
     * Upload original file
72
     */
73
    protected function uploadOriginalFile(string $id, ?string $disk = null): void
74
    {
75
        Storage::disk($disk ?: $this->disk)
76
            ->putFileAs(
77
                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

77
                path: $this->/** @scrutinizer ignore-call */ getBasePath($id, Larupload::ORIGINAL_FOLDER),
Loading history...
78
                file: $this->file,
79
                name: $this->output['name']
80
            );
81
    }
82
83
    /**
84
     * Clean directory before upload
85
     */
86
    protected function clean($id): void
87
    {
88
        $path = $this->getBasePath($id);
89
        Storage::disk($this->disk)->deleteDirectory($path);
90
91
        foreach ($this->output as $key => $value) {
92
            $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...
93
        }
94
    }
95
}
96