Test Failed
Push — master ( 9bd3ec...26cfa8 )
by Mostafa
10:02 queued 05:48
created

StyleAttachment::handleStyles()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 8
nop 3
dl 0
loc 36
rs 8.6506
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
    }
59
60
    /**
61
     * Handle styles for videos
62
     *
63
     * @param $id
64
     */
65
    protected function handleVideoStyles($id): void
66
    {
67
        foreach ($this->videoStyles as $name => $style) {
68
            $path = $this->getBasePath($id, $name);
69
            Storage::disk($this->disk)->makeDirectory($path);
70
            $saveTo = "$path/{$this->output['name']}";
71
72
            $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

72
            $this->/** @scrutinizer ignore-call */ 
73
                   ffmpeg()->manipulate($style, $saveTo);
Loading history...
73
        }
74
75
        if (count($this->streams)) {
76
            $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

76
            $fileName = /** @scrutinizer ignore-type */ pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8';
Loading history...
77
78
            $path = $this->getBasePath($id, Larupload::STREAM_FOLDER);
79
            Storage::disk($this->disk)->makeDirectory($path);
80
81
            $this->ffmpeg()->stream($this->streams, $path, $fileName);
82
        }
83
    }
84
85
    /**
86
     * Prepare style path
87
     * this function will use to prepare full path of given style to generate url/download response
88
     *
89
     * @param string $style
90
     * @return string|null
91
     */
92
    protected function prepareStylePath(string $style): ?string
93
    {
94
        $staticStyles = [
95
            Larupload::ORIGINAL_FOLDER,
96
            Larupload::COVER_FOLDER,
97
            Larupload::STREAM_FOLDER
98
        ];
99
100
        if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles))) {
101
            $name = $style == Larupload::COVER_FOLDER
102
                ? $this->output['cover']
103
                : $this->output['name'];
104
105
            $type = $this->output['type']
106
                ? LaruploadFileType::from($this->output['type'])
107
                : null;
108
109
            if ($name and $style == Larupload::STREAM_FOLDER) {
110
                if ($type === LaruploadFileType::VIDEO) {
111
                    $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

111
                    $name = /** @scrutinizer ignore-type */ pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
Loading history...
112
                    $path = $this->getBasePath($this->id, $style);
113
114
                    return "$path/$name";
115
                }
116
117
                return null;
118
            }
119
            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

119
            else if ($name and $this->/** @scrutinizer ignore-call */ styleHasFile($style)) {
Loading history...
120
                $name = FixExceptionNamesAction::make($name, $style)->run();
121
                $path = $this->getBasePath($this->id, $style);
122
123
                return "$path/$name";
124
            }
125
        }
126
127
        return null;
128
    }
129
}
130