Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
23 / 23 |
GenerateFile | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
23 / 23 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
handle | |
100.00% |
1 / 1 |
2 | |
100.00% |
14 / 14 |
|||
getErrorMessage | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\FileGenerator\Jobs; |
4 | |
5 | use Exception; |
6 | use Illuminate\Support\Facades\Redis; |
7 | use Qmp\Laravel\FileGenerator\Config; |
8 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Factory; |
9 | use Illuminate\Bus\Queueable; |
10 | use Illuminate\Queue\SerializesModels; |
11 | use Illuminate\Queue\InteractsWithQueue; |
12 | use Illuminate\Contracts\Queue\ShouldQueue; |
13 | use Illuminate\Foundation\Bus\Dispatchable; |
14 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess; |
15 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess; |
16 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
17 | |
18 | class GenerateFile extends \Jobiden implements ShouldQueue |
19 | { |
20 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Timer; |
21 | |
22 | /** |
23 | * Undocumented variable |
24 | * |
25 | * @var integer |
26 | */ |
27 | public $tries = 3; |
28 | |
29 | /** |
30 | * @var \Qmp\Laravel\FileGenerator\Config |
31 | */ |
32 | protected Config $config; |
33 | |
34 | /** |
35 | * Create a new job instance. |
36 | * |
37 | * @return void |
38 | */ |
39 | public function __construct(Config $config) |
40 | { |
41 | $this->config = $config; |
42 | |
43 | $this->middleware = [ |
44 | CreateParentProcess::class, |
45 | UpdateParentProcess::class |
46 | ]; |
47 | |
48 | $this->onConnection('file-generator'); |
49 | } |
50 | |
51 | /** |
52 | * Execute the job. |
53 | * |
54 | * @return void |
55 | */ |
56 | public function handle() |
57 | { |
58 | $this->startTimer('total'); |
59 | |
60 | $data = $this->middlewareData(UpdateParentProcess::class) |
61 | ->transfert(CreateParentProcess::class, ['id']); |
62 | |
63 | try { |
64 | |
65 | $generator = Factory::handle($this->config); |
66 | $path = $generator->generate(); |
67 | |
68 | Redis::publish($this->config->getPubsubKey('channel'), json_encode(array_merge($this->config->getPubsubKey('message'), ['path' => $path]))); |
69 | |
70 | $data->log = [ |
71 | 'duration' => $this->getTimer('total'), |
72 | ]; |
73 | |
74 | } catch (Exception $e) { |
75 | Redis::publish('file-generator', json_encode([ |
76 | 'action' => 'handle-error', |
77 | 'message' => $this->getErrorMessage($e) |
78 | ])); |
79 | |
80 | $data->error = $this->reportException($e, true); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * @param \Exception $e |
86 | * @return array |
87 | */ |
88 | protected function getErrorMessage(Exception $e): array |
89 | { |
90 | return array_merge($this->config->toArray(), [ |
91 | 'error' => [ |
92 | 'exception' => get_class($e), |
93 | 'message' => $e->getMessage(), |
94 | 'line' => $e->getLine(), |
95 | 'file' => $e->getFile() |
96 | ] |
97 | ]); |
98 | } |
99 | } |