PdfOptimizerJob::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 1
rs 10
1
<?php
2
3
namespace Mostafaznv\PdfOptimizer\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
use Mostafaznv\PdfOptimizer\Actions\OptimizePdfAction;
11
use Mostafaznv\PdfOptimizer\DTOs\PdfOptimizerJobData;
12
use Mostafaznv\PdfOptimizer\Events\PdfOptimizerJobFinished;
13
14
15
class PdfOptimizerJob implements ShouldQueue
16
{
17
    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\PdfOptimizer\Jobs\PdfOptimizerJob: $collectionClass, $id, $relations, $class, $keyBy
Loading history...
introduced by
The trait Illuminate\Queue\InteractsWithQueue requires some properties which are not provided by Mostafaznv\PdfOptimizer\Jobs\PdfOptimizerJob: $failedWith, $releaseDelay
Loading history...
18
19
    public function __construct(private readonly PdfOptimizerJobData $data) {}
20
21
22
    public function handle(): void
23
    {
24
        $this->data->logger?->info("Job {$this->data->id} started.");
25
26
        $result = OptimizePdfAction::init($this->data->file, $this->data->disk)
27
            ->logger($this->data->logger)
28
            ->setTimeout($this->data->timeout)
29
            ->execute(
30
                $this->data->commands,
31
                $this->data->input,
32
                $this->data->output
33
            );
34
35
        event(
36
            new PdfOptimizerJobFinished($this->data->id, $result->status, $result->message)
37
        );
38
39
        if ($result->status) {
40
            $this->data->logger?->info('Job finished successfully.');
41
        }
42
        else {
43
            $this->data->logger?->error('Job finished with error.');
44
45
            $this->fail($result->message);
46
        }
47
    }
48
}
49