Test Failed
Push — master ( 7e7df5...676302 )
by Mostafa
07:11 queued 03:20
created

ProcessFFMpeg::handle()   A

Complexity

Conditions 5
Paths 15

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 3
b 0
f 0
nc 15
nop 0
dl 0
loc 41
rs 9.2408
1
<?php
2
3
namespace Mostafaznv\Larupload\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Exception;
8
use Illuminate\Queue\SerializesModels;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Contracts\Queue\ShouldQueue;
11
use Illuminate\Foundation\Bus\Dispatchable;
12
use Illuminate\Support\Facades\DB;
13
use Mostafaznv\Larupload\Events\LaruploadFFMpegQueueFinished;
14
use Mostafaznv\Larupload\Larupload;
15
use Mostafaznv\Larupload\LaruploadEnum;
16
17
class ProcessFFMpeg implements ShouldQueue
18
{
19
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Mostafaznv\Larupload\Jobs\ProcessFFMpeg: $relations, $class, $keyBy
Loading history...
20
21
    protected int       $queueId;
22
    protected int       $id;
23
    protected string    $name;
24
    protected string    $model;
25
    protected ?Larupload $standalone = null;
26
27
28
    public function __construct(int $queueId, int $id, string $name, string $model, ?string $standalone = null)
29
    {
30
        $this->queueId = $queueId;
31
        $this->id = $id;
32
        $this->name = $name;
33
        $this->model = $model;
34
35
        if ($standalone) {
36
            $this->standalone = unserialize(base64_decode($standalone));
37
        }
38
    }
39
40
    /**
41
     * @throws Exception
42
     */
43
    public function handle()
44
    {
45
        $this->updateStatus(false, true);
46
47
        // we need to handle ffmpeg queue after model saved event
48
        sleep(1);
49
50
        try {
51
            if ($this->standalone) {
52
                $this->standalone->handleFFMpegQueue();
53
            }
54
            else {
55
                /** @var \Illuminate\Database\Eloquent\Model $class */
56
                $class = $this->model;
57
                $modelNotSaved = true;
58
59
                while ($modelNotSaved) {
60
                    $model = $class::where('id', $this->id)->first();
61
62
                    if ($model->{$this->name}->meta('name')) {
63
                        $modelNotSaved = false;
64
65
                        $availableQueues = DB::table(LaruploadEnum::FFMPEG_QUEUE_TABLE)
66
                            ->where('record_id', $this->id)
67
                            ->where('record_class', $this->model)
68
                            ->where('status', false)
69
                            ->count();
70
71
                        $model->{$this->name}->handleFFMpegQueue($availableQueues === 1);
72
                    }
73
74
                    sleep(1);
75
                }
76
            }
77
78
            $this->updateStatus(true, false);
79
        }
80
        catch (FileNotFoundException | Exception $e) {
81
            $this->updateStatus(false, false, $e->getMessage());
82
83
            throw new Exception($e->getMessage());
84
        }
85
    }
86
87
    /**
88
     * Update LaruploadFFMpegQueue table
89
     *
90
     * @param bool $status
91
     * @param bool $isStarted
92
     * @param string|null $message
93
     * @return int
94
     */
95
    protected function updateStatus(bool $status, bool $isStarted, string $message = null): int
96
    {
97
        $dateColumn = $isStarted ? 'started_at' : 'finished_at';
98
99
        $result = DB::table(LaruploadEnum::FFMPEG_QUEUE_TABLE)->where('id', $this->queueId)->update([
100
            'status'    => $status,
101
            'message'   => $message,
102
            $dateColumn => now(),
103
        ]);
104
105
        if ($result and $status) {
106
            event(new LaruploadFFMpegQueueFinished($this->id, $this->model, $this->queueId));
107
        }
108
109
        return $result;
110
    }
111
}
112