Test Failed
Push — master ( ba03f2...dee2b3 )
by Mostafa
58s queued 15s
created

StyleAttachment::handleAudioStyles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Enums\LaruploadFileType;
8
use Mostafaznv\Larupload\Larupload;
9
use Mostafaznv\Larupload\Actions\FixExceptionNamesAction;
10
11
trait StyleAttachment
12
{
13
    /**
14
     * Handle styles
15
     * resize, crop and generate styles from original file
16
     *
17
     * @param string $id
18
     * @param Model|string $class
19
     * @param bool $standalone
20
     */
21
    protected function handleStyles(string $id, Model|string $model, bool $standalone = false): void
22
    {
23
        switch ($this->type) {
24
            case LaruploadFileType::IMAGE:
25
                foreach ($this->imageStyles as $name => $style) {
26
                    $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

26
                    /** @scrutinizer ignore-call */ 
27
                    $path = $this->getBasePath($id, $name);
Loading history...
27
                    $saveTo = $path . '/' . FixExceptionNamesAction::make($this->output['name'], $name)->run();
28
29
                    Storage::disk($this->disk)->makeDirectory($path);
30
                    $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

30
                    $this->/** @scrutinizer ignore-call */ 
31
                           img($this->file)->resize($saveTo, $style);
Loading history...
31
                }
32
33
                break;
34
35
            case LaruploadFileType::VIDEO:
36
                if ($this->ffmpegQueue) {
37
                    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

37
                    if ($this->/** @scrutinizer ignore-call */ driverIsNotLocal()) {
Loading history...
38
                        $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

38
                        $this->/** @scrutinizer ignore-call */ 
39
                               uploadOriginalFile($id, $this->localDisk);
Loading history...
39
                    }
40
41
                    if ($model instanceof Model) {
42
                        $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

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

95
            $this->/** @scrutinizer ignore-call */ 
96
                   ffmpeg()->manipulate($style, $saveTo);
Loading history...
96
        }
97
98
        if (count($this->streams)) {
99
            $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

99
            $fileName = /** @scrutinizer ignore-type */ pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8';
Loading history...
100
101
            $path = $this->getBasePath($id, Larupload::STREAM_FOLDER);
102
            Storage::disk($this->disk)->makeDirectory($path);
103
104
            $this->ffmpeg()->stream($this->streams, $path, $fileName);
105
        }
106
    }
107
108
    /**
109
     * Handle styles for audios
110
     *
111
     * @param $id
112
     */
113
    protected function handleAudioStyles($id): void
114
    {
115
        foreach ($this->audioStyles as $name => $style) {
116
            $path = $this->getBasePath($id, $name);
117
            Storage::disk($this->disk)->makeDirectory($path);
118
            $saveTo = "$path/{$this->output['name']}";
119
120
            $this->ffmpeg()->audio($style, $saveTo);
121
        }
122
    }
123
124
    /**
125
     * Prepare style path
126
     * this function will use to prepare full path of given style to generate url/download response
127
     *
128
     * @param string $style
129
     * @return string|null
130
     */
131
    protected function prepareStylePath(string $style): ?string
132
    {
133
        $staticStyles = [
134
            Larupload::ORIGINAL_FOLDER,
135
            Larupload::COVER_FOLDER,
136
            Larupload::STREAM_FOLDER
137
        ];
138
139
        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))) {
140
            $name = $style == Larupload::COVER_FOLDER
141
                ? $this->output['cover']
142
                : $this->output['name'];
143
144
            $type = $this->output['type']
145
                ? LaruploadFileType::from($this->output['type'])
146
                : null;
147
148
            if ($name and $style == Larupload::STREAM_FOLDER) {
149
                if ($type === LaruploadFileType::VIDEO) {
150
                    $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

150
                    $name = /** @scrutinizer ignore-type */ pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
Loading history...
151
                    $path = $this->getBasePath($this->id, $style);
152
153
                    return "$path/$name";
154
                }
155
156
                return null;
157
            }
158
            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

158
            else if ($name and $this->/** @scrutinizer ignore-call */ styleHasFile($style)) {
Loading history...
159
160
                $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

160
                $name = FixExceptionNamesAction::make($name, $style, $this->/** @scrutinizer ignore-call */ getStyle($style))->run();
Loading history...
161
                $path = $this->getBasePath($this->id, $style);
162
163
                return "$path/$name";
164
            }
165
        }
166
167
        return null;
168
    }
169
}
170