QueueAttachment::prepareFileForFFMpegProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
use Illuminate\Http\Exceptions\HttpResponseException;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Facades\URL;
10
use Mostafaznv\Larupload\Actions\GuessLaruploadFileTypeAction;
11
use Mostafaznv\Larupload\Enums\LaruploadFileType;
12
use Mostafaznv\Larupload\Jobs\ProcessFFMpeg;
13
use Mostafaznv\Larupload\Larupload;
14
15
16
trait QueueAttachment
17
{
18
    /**
19
     * Handle FFMpeg queue on running ffmpeg queue:work
20
     */
21
    public function handleFFMpegQueue(bool $isLastOne = false, bool $standalone = false): void
22
    {
23
        $this->file = $this->prepareFileForFFMpegProcess();
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        $this->type = GuessLaruploadFileTypeAction::make($this->file)->calc();
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
26
        if ($this->type == LaruploadFileType::VIDEO) {
27
            $this->handleVideoStyles($this->id);
0 ignored issues
show
Bug introduced by
It seems like handleVideoStyles() 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

27
            $this->/** @scrutinizer ignore-call */ 
28
                   handleVideoStyles($this->id);
Loading history...
28
        }
29
        else if ($this->type == LaruploadFileType::AUDIO) {
30
            $this->handleAudioStyles($this->id);
0 ignored issues
show
Bug introduced by
It seems like handleAudioStyles() 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
                   handleAudioStyles($this->id);
Loading history...
31
        }
32
33
        if ($this->driverIsNotLocal() and $isLastOne) {
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

33
        if ($this->/** @scrutinizer ignore-call */ driverIsNotLocal() and $isLastOne) {
Loading history...
34
            Storage::disk($this->localDisk)->deleteDirectory(
35
                $standalone ? "$this->name" : "$this->folder/$this->id"
36
            );
37
        }
38
    }
39
40
    protected function initializeFFMpegQueue(int $id, string $class, bool $standalone = false): void
41
    {
42
        $maxQueueNum = $this->ffmpegMaxQueueNum;
43
        $flag = false;
44
45
        if ($maxQueueNum == 0) {
46
            $flag = true;
47
        }
48
        else {
49
            $availableQueues = DB::table(Larupload::FFMPEG_QUEUE_TABLE)
50
                ->where('status', 0)
51
                ->count();
52
53
            if ($availableQueues < $maxQueueNum) {
54
                $flag = true;
55
            }
56
        }
57
58
59
        if ($flag) {
60
            // save a copy of original file to use it on process ffmpeg queue, then delete it
61
            if ($this->driverIsNotLocal()) {
62
                $path = $this->getBasePath($id, Larupload::ORIGINAL_FOLDER);
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

62
                /** @scrutinizer ignore-call */ 
63
                $path = $this->getBasePath($id, Larupload::ORIGINAL_FOLDER);
Loading history...
63
                Storage::disk($this->localDisk)->putFileAs($path, $this->file, $this->output['name']);
64
            }
65
66
            $queueId = DB::table(Larupload::FFMPEG_QUEUE_TABLE)->insertGetId([
67
                'record_id'    => $id,
68
                'record_class' => $class,
69
                'created_at'   => now(),
70
            ]);
71
72
            $serializedClass = null;
73
74
            if ($standalone) {
75
                unset($this->file);
76
                unset($this->cover);
77
                unset($this->image);
78
                unset($this->ffmpeg);
79
80
                $serializedClass = base64_encode(serialize($this));
81
            }
82
83
84
            ProcessFFMpeg::dispatch($queueId, $id, $this->name, $class, $serializedClass);
85
        }
86
        else {
87
            throw new HttpResponseException(redirect(URL::previous())->withErrors([
88
                'ffmpeg_queue_max_num' => trans('larupload::messages.max-queue-num-exceeded')
89
            ]));
90
        }
91
    }
92
93
    private function prepareFileForFFMpegProcess(): UploadedFile
94
    {
95
        $basePath = $this->getBasePath($this->id, Larupload::ORIGINAL_FOLDER);
96
        $path = $basePath . '/' . $this->output['name'];
97
        $disk = $this->driverIsLocal() ? $this->disk : $this->localDisk;
0 ignored issues
show
Bug introduced by
It seems like driverIsLocal() 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

97
        $disk = $this->/** @scrutinizer ignore-call */ driverIsLocal() ? $this->disk : $this->localDisk;
Loading history...
98
99
        $path = Storage::disk($disk)->path($path);
100
101
        return new UploadedFile($path, $this->output['name'], null, null, true);
102
    }
103
}
104