Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

GeneratePDF::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 36
rs 9.584
c 1
b 0
f 1
1
<?php
2
3
namespace App\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Queue\SerializesModels;
7
use Illuminate\Queue\InteractsWithQueue;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Foundation\Bus\Dispatchable;
10
use App\Models\Eloquent\ContestModel as EloquentContestModel;
11
use Imtigger\LaravelJobStatus\Trackable;
12
use PDF;
13
14
class GeneratePDF implements ShouldQueue
15
{
16
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable;
0 ignored issues
show
Bug introduced by
The trait Imtigger\LaravelJobStatus\Trackable requires the property $id which is not provided by App\Jobs\GeneratePDF.
Loading history...
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\GeneratePDF: $id, $relations, $class, $keyBy
Loading history...
17
18
    public $tries = 5;
19
    protected $cid;
20
    protected $config;
21
22
    /**
23
     * Create a new job instance.
24
     *
25
     * @return void
26
     */
27
28
    public function __construct($cid, $config)
29
    {
30
        $this->prepareStatus();
31
        $this->cid=$cid;
32
        $default=[
33
            'cover'=>false,
34
            'advice'=>false,
35
        ];
36
        $this->config=array_merge($default,$config);
37
    }
38
39
    /**
40
     * Execute the job.
41
     *
42
     * @return void
43
     */
44
    public function handle()
45
    {
46
        $cid=$this->cid;
47
        $config=$this->config;
48
49
        if (!is_dir(storage_path("app/contest/pdf/"))){
50
            mkdir(storage_path("app/contest/pdf/"), 0777, true);
51
        }
52
53
        $record=EloquentContestModel::find($cid);
54
        // dd(EloquentContestModel::getProblemSet($cid));
55
56
        $pdf=PDF::setOptions([
57
            'dpi' => 150,
58
            'isPhpEnabled' => true,
59
            'isHtml5ParserEnabled' => true,
60
            'isRemoteEnabled' => true
61
        ])->setWarnings(true)->loadView('pdf.contest.main', [
62
            'conf'=>$config,
63
            'contest' => [
64
                'cid'=>$cid,
65
                'name'=>$record->name,
66
                'shortName'=>$record->name,
67
                'date'=>date("F j, Y", strtotime($record->begin_time)),
68
            ],
69
            'problemset'=>EloquentContestModel::getProblemSet($cid, true),
70
        ]);
71
        // $pdf->getDomPDF()->add_info('Subject', "$record->name ProblemSet");
72
        // $pdf->getDomPDF()->add_info('Producer', config('app.displayName'));
73
        // $pdf->getDomPDF()->add_info('Creator', config('app.name').' Contest PDF Auto-Generater');
74
        // $pdf->getDomPDF()->add_info('CreatorTool', config('app.url'));
75
        // $pdf->getDomPDF()->add_info('BaseURL', route('contest.detail',['cid'=>$cid]));
76
        $pdf->save(storage_path("app/contest/pdf/$cid.pdf"));
77
78
        $record->pdf=1;
79
        $record->save();
80
    }
81
82
    public function failed()
83
    {
84
85
    }
86
}
87