Test Failed
Pull Request — master (#31)
by
unknown
04:02
created

StyleAttachment::handleAudioStyles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
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 1
dl 0
loc 14
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\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
    static function get_class_name($classname)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
    {
110
        if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1);
111
        return (string) $pos;
112
    }
113
114
    /**
115
     * Handle styles for audio
116
     *
117
     * @param $id
118
     */
119
    protected function handleAudioStyles($id): void
120
    {
121
        foreach ($this->audioStyles as $name => $style) {
122
            $path = $this->getBasePath($id, $name);
123
            Storage::disk($this->disk)->makeDirectory($path);
124
125
            $className = get_class($style->format);
126
            $baseClassName = basename(str_replace('\\', '/', $className));
127
            $ext = '.' . strtolower($baseClassName);
128
129
            $this->output['name'] = pathinfo($this->output['name'], PATHINFO_FILENAME) . $ext;
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...
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

129
            $this->output['name'] = /** @scrutinizer ignore-type */ pathinfo($this->output['name'], PATHINFO_FILENAME) . $ext;
Loading history...
130
            $saveTo = "$path/{$this->output['name']}";
131
132
            $this->ffmpeg()->audio($style, $saveTo);
133
        }
134
    }
135
136
    /**
137
     * Prepare style path
138
     * this function will use to prepare full path of given style to generate url/download response
139
     *
140
     * @param string $style
141
     * @return string|null
142
     */
143
    protected function prepareStylePath(string $style): ?string
144
    {
145
        $staticStyles = [
146
            Larupload::ORIGINAL_FOLDER,
147
            Larupload::COVER_FOLDER,
148
            Larupload::STREAM_FOLDER
149
        ];
150
151
        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))) {
152
            $name = $style == Larupload::COVER_FOLDER
153
                ? $this->output['cover']
154
                : $this->output['name'];
155
156
            $type = $this->output['type']
157
                ? LaruploadFileType::from($this->output['type'])
158
                : null;
159
160
            if ($name and $style == Larupload::STREAM_FOLDER) {
161
                if ($type === LaruploadFileType::VIDEO) {
162
                    $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

162
                    $name = /** @scrutinizer ignore-type */ pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
Loading history...
163
                    $path = $this->getBasePath($this->id, $style);
164
165
                    return "$path/$name";
166
                }
167
168
                return null;
169
            }
170
            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

170
            else if ($name and $this->/** @scrutinizer ignore-call */ styleHasFile($style)) {
Loading history...
171
                $name = FixExceptionNamesAction::make($name, $style)->run();
172
                $path = $this->getBasePath($this->id, $style);
173
174
                return "$path/$name";
175
            }
176
        }
177
178
        return null;
179
    }
180
}
181