StyleAttachment::prepareStylePath()   C
last analyzed

Complexity

Conditions 13
Paths 8

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 22
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 36
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
use FFMpeg\Format\Audio\Aac;
6
use FFMpeg\Format\Audio\Flac;
7
use FFMpeg\Format\Audio\Mp3;
8
use FFMpeg\Format\Audio\Wav;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Support\Facades\Storage;
11
use Mostafaznv\Larupload\DTOs\Style\AudioStyle;
12
use Mostafaznv\Larupload\Enums\LaruploadFileType;
13
use Mostafaznv\Larupload\Larupload;
14
use Mostafaznv\Larupload\Actions\FixExceptionNamesAction;
15
16
trait StyleAttachment
17
{
18
    /**
19
     * Handle styles
20
     * resize, crop and generate styles from original file
21
     *
22
     * @param string $id
23
     * @param Model|string $class
24
     * @param bool $standalone
25
     */
26
    protected function handleStyles(string $id, Model|string $model, bool $standalone = false): void
27
    {
28
        switch ($this->type) {
29
            case LaruploadFileType::IMAGE:
30
                foreach ($this->imageStyles as $name => $style) {
31
                    $path = $this->getBasePath($id, $name);
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

31
                    /** @scrutinizer ignore-call */ 
32
                    $path = $this->getBasePath($id, $name);
Loading history...
32
                    $saveTo = $path . '/' . FixExceptionNamesAction::make($this->output['name'], $name)->run();
33
34
                    Storage::disk($this->disk)->makeDirectory($path);
35
                    $this->img($this->file)->resize($saveTo, $style);
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

35
                    $this->/** @scrutinizer ignore-call */ 
36
                           img($this->file)->resize($saveTo, $style);
Loading history...
36
                }
37
38
                break;
39
40
            case LaruploadFileType::VIDEO:
41
                if ($this->ffmpegQueue) {
42
                    if ($this->driverIsNotLocal()) {
0 ignored issues
show
Bug introduced by
It seems like driverIsNotLocal() 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

42
                    if ($this->/** @scrutinizer ignore-call */ driverIsNotLocal()) {
Loading history...
43
                        $this->uploadOriginalFile($id, $this->localDisk);
0 ignored issues
show
Bug introduced by
It seems like uploadOriginalFile() 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

43
                        $this->/** @scrutinizer ignore-call */ 
44
                               uploadOriginalFile($id, $this->localDisk);
Loading history...
44
                    }
45
46
                    if ($model instanceof Model) {
47
                        $this->initializeFFMpegQueue(
0 ignored issues
show
Bug introduced by
It seems like initializeFFMpegQueue() 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

47
                        $this->/** @scrutinizer ignore-call */ 
48
                               initializeFFMpegQueue(
Loading history...
48
                            $model->id, $model->getMorphClass(), $standalone
49
                        );
50
                    }
51
                    else {
52
                        $this->initializeFFMpegQueue(
53
                            $id, $model, $standalone
54
                        );
55
                    }
56
                }
57
                else {
58
                    $this->handleVideoStyles($id);
59
                }
60
61
                break;
62
63
            case LaruploadFileType::AUDIO:
64
                if ($this->ffmpegQueue) {
65
                    if ($this->driverIsNotLocal()) {
66
                        $this->uploadOriginalFile($id, $this->localDisk);
67
                    }
68
69
                    if ($model instanceof Model) {
70
                        $this->initializeFFMpegQueue(
71
                            $model->id, $model->getMorphClass(), $standalone
72
                        );
73
                    }
74
                    else {
75
                        $this->initializeFFMpegQueue(
76
                            $id, $model, $standalone
77
                        );
78
                    }
79
                }
80
                else {
81
                    $this->handleAudioStyles($id);
82
                }
83
84
                break;
85
        }
86
    }
87
88
    /**
89
     * Handle styles for videos
90
     *
91
     * @param $id
92
     */
93
    protected function handleVideoStyles($id): void
94
    {
95
        foreach ($this->videoStyles as $name => $style) {
96
            $path = $this->getBasePath($id, $name);
97
            Storage::disk($this->disk)->makeDirectory($path);
98
99
            $saveTo = "$path/{$this->output['name']}";
100
101
102
            if ($style->isAudioFormat()) {
103
                $this->ffmpeg(null, $style)->audio(
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

103
                $this->/** @scrutinizer ignore-call */ 
104
                       ffmpeg(null, $style)->audio(
Loading history...
104
                    style: AudioStyle::make($style->name, $style->format),
105
                    saveTo: $saveTo
106
                );
107
108
                continue;
109
            }
110
111
            $this->ffmpeg(null, $style)->manipulate($style, $saveTo);
112
        }
113
114
        if (count($this->streams)) {
115
            $fileName = pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8';
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($this->output['...ment\PATHINFO_FILENAME) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

115
            $fileName = /** @scrutinizer ignore-type */ pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8';
Loading history...
116
117
            $path = $this->getBasePath($id, Larupload::STREAM_FOLDER);
118
            Storage::disk($this->disk)->makeDirectory($path);
119
120
            $this->ffmpeg()->stream($this->streams, $path, $fileName);
121
        }
122
    }
123
124
    /**
125
     * Handle styles for audios
126
     *
127
     * @param $id
128
     */
129
    protected function handleAudioStyles($id): void
130
    {
131
        foreach ($this->audioStyles as $name => $style) {
132
            $path = $this->getBasePath($id, $name);
133
            Storage::disk($this->disk)->makeDirectory($path);
134
            $saveTo = "$path/{$this->output['name']}";
135
136
            $this->ffmpeg()->audio($style, $saveTo);
137
        }
138
    }
139
140
    /**
141
     * Prepare style path
142
     * this function will use to prepare full path of given style to generate url/download response
143
     *
144
     * @param string $style
145
     * @return string|null
146
     */
147
    protected function prepareStylePath(string $style): ?string
148
    {
149
        $staticStyles = [
150
            Larupload::ORIGINAL_FOLDER,
151
            Larupload::COVER_FOLDER,
152
            Larupload::STREAM_FOLDER
153
        ];
154
155
        if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles) or array_key_exists($style, $this->audioStyles))) {
156
            $name = $style == Larupload::COVER_FOLDER
157
                ? $this->output['cover']
158
                : $this->output['name'];
159
160
            $type = $this->output['type']
161
                ? LaruploadFileType::from($this->output['type'])
162
                : null;
163
164
            if ($name and $style == Larupload::STREAM_FOLDER) {
165
                if ($type === LaruploadFileType::VIDEO) {
166
                    $name = pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($name, Mostafaz...ment\PATHINFO_FILENAME) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

166
                    $name = /** @scrutinizer ignore-type */ pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
Loading history...
167
                    $path = $this->getBasePath($this->id, $style);
168
169
                    return "$path/$name";
170
                }
171
172
                return null;
173
            }
174
            else if ($name and $this->styleHasFile($style)) {
0 ignored issues
show
Bug introduced by
It seems like styleHasFile() 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

174
            else if ($name and $this->/** @scrutinizer ignore-call */ styleHasFile($style)) {
Loading history...
175
                $name = FixExceptionNamesAction::make($name, $style, $this->getStyle($style))->run();
0 ignored issues
show
Bug introduced by
It seems like getStyle() 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

175
                $name = FixExceptionNamesAction::make($name, $style, $this->/** @scrutinizer ignore-call */ getStyle($style))->run();
Loading history...
176
                $path = $this->getBasePath($this->id, $style);
177
178
                return "$path/$name";
179
            }
180
        }
181
182
        return null;
183
    }
184
}
185