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

StyleAttachment   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
c 0
b 0
f 0
dl 0
loc 108
rs 10
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
C prepareStylePath() 0 36 12
A handleVideoStyles() 0 17 3
A handleStyles() 0 27 6
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
use Illuminate\Support\Facades\Storage;
6
use Mostafaznv\Larupload\Enums\LaruploadFileType;
7
use Mostafaznv\Larupload\Larupload;
8
use Mostafaznv\Larupload\Actions\FixExceptionNamesAction;
9
10
trait StyleAttachment
11
{
12
    /**
13
     * Handle styles
14
     * resize, crop and generate styles from original file
15
     *
16
     * @param string $id
17
     * @param string $class
18
     * @param bool $standalone
19
     */
20
    protected function handleStyles(string $id, string $class, bool $standalone = false): void
21
    {
22
        switch ($this->type) {
23
            case LaruploadFileType::IMAGE:
24
                foreach ($this->imageStyles as $name => $style) {
25
                    $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

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

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

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

37
                        $this->/** @scrutinizer ignore-call */ 
38
                               uploadOriginalFile($id, $this->localDisk);
Loading history...
38
                    }
39
40
                    $this->initializeFFMpegQueue($id, $class, $standalone);
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

40
                    $this->/** @scrutinizer ignore-call */ 
41
                           initializeFFMpegQueue($id, $class, $standalone);
Loading history...
41
                }
42
                else {
43
                    $this->handleVideoStyles($id);
44
                }
45
46
                break;
47
        }
48
    }
49
50
    /**
51
     * Handle styles for videos
52
     *
53
     * @param $id
54
     */
55
    protected function handleVideoStyles($id): void
56
    {
57
        foreach ($this->videoStyles as $name => $style) {
58
            $path = $this->getBasePath($id, $name);
59
            Storage::disk($this->disk)->makeDirectory($path);
60
            $saveTo = "$path/{$this->output['name']}";
61
62
            $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

62
            $this->/** @scrutinizer ignore-call */ 
63
                   ffmpeg()->manipulate($style, $saveTo);
Loading history...
63
        }
64
65
        if (count($this->streams)) {
66
            $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

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

101
                    $name = /** @scrutinizer ignore-type */ pathinfo($name, PATHINFO_FILENAME) . '.m3u8';
Loading history...
102
                    $path = $this->getBasePath($this->id, $style);
103
104
                    return "$path/$name";
105
                }
106
107
                return null;
108
            }
109
            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

109
            else if ($name and $this->/** @scrutinizer ignore-call */ styleHasFile($style)) {
Loading history...
110
                $name = FixExceptionNamesAction::make($name, $style)->run();
111
                $path = $this->getBasePath($this->id, $style);
112
113
                return "$path/$name";
114
            }
115
        }
116
117
        return null;
118
    }
119
}
120