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

QueueAttachment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
c 0
b 0
f 0
dl 0
loc 81
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handleFFMpegQueue() 0 10 4
A prepareFileForFFMpegProcess() 0 9 2
B initializeFFMpegQueue() 0 49 6
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment;
4
5
6
use Illuminate\Http\Exceptions\HttpResponseException;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Storage;
10
use Illuminate\Support\Facades\URL;
11
use Mostafaznv\Larupload\Actions\GuessLaruploadFileTypeAction;
12
use Mostafaznv\Larupload\Jobs\ProcessFFMpeg;
13
use Mostafaznv\Larupload\Larupload;
14
15
trait QueueAttachment
16
{
17
    /**
18
     * Handle FFMpeg queue on running ffmpeg queue:work
19
     */
20
    public function handleFFMpegQueue(bool $isLastOne = false, bool $standalone = false): void
21
    {
22
        $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...
23
        $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...
24
25
        $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

25
        $this->/** @scrutinizer ignore-call */ 
26
               handleVideoStyles($this->id);
Loading history...
26
27
        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

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

56
                /** @scrutinizer ignore-call */ 
57
                $path = $this->getBasePath($id, Larupload::ORIGINAL_FOLDER);
Loading history...
57
                Storage::disk($this->localDisk)->putFileAs($path, $this->file, $this->output['name']);
58
            }
59
60
            $queueId = DB::table(Larupload::FFMPEG_QUEUE_TABLE)->insertGetId([
61
                'record_id'    => $id,
62
                'record_class' => $class,
63
                'created_at'   => now(),
64
            ]);
65
66
            $serializedClass = null;
67
68
            if ($standalone) {
69
                unset($this->file);
70
                unset($this->cover);
71
                unset($this->image);
72
                unset($this->ffmpeg);
73
74
                $serializedClass = base64_encode(serialize($this));
75
            }
76
77
78
            ProcessFFMpeg::dispatch($queueId, $id, $this->name, $class, $serializedClass);
79
        }
80
        else {
81
            throw new HttpResponseException(redirect(URL::previous())->withErrors([
0 ignored issues
show
Bug introduced by
It seems like redirect(Illuminate\Supp...-queue-num-exceeded'))) can also be of type Illuminate\Routing\Redirector; however, parameter $response of Illuminate\Http\Exceptio...xception::__construct() does only seem to accept Symfony\Component\HttpFoundation\Response, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            throw new HttpResponseException(/** @scrutinizer ignore-type */ redirect(URL::previous())->withErrors([
Loading history...
82
                'ffmpeg_queue_max_num' => trans('larupload::messages.max-queue-num-exceeded')
83
            ]));
84
        }
85
    }
86
87
    private function prepareFileForFFMpegProcess(): UploadedFile
88
    {
89
        $basePath = $this->getBasePath($this->id, Larupload::ORIGINAL_FOLDER);
90
        $path = $basePath . '/' . $this->output['name'];
91
        $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

91
        $disk = $this->/** @scrutinizer ignore-call */ driverIsLocal() ? $this->disk : $this->localDisk;
Loading history...
92
93
        $path = Storage::disk($disk)->path($path);
94
95
        return new UploadedFile($path, $this->output['name'], null, null, true);
96
    }
97
}
98