Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MessagesJob | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace App\Jobs; |
4 | |
5 | use App\Services\RabbitMQService; |
6 | use Illuminate\Bus\Queueable; |
7 | use Illuminate\Contracts\Queue\ShouldQueue; |
8 | use Illuminate\Foundation\Bus\Dispatchable; |
9 | use Illuminate\Queue\InteractsWithQueue; |
10 | use Illuminate\Queue\SerializesModels; |
11 | |
12 | class MessagesJob implements ShouldQueue |
13 | { |
14 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
15 | |
16 | protected mixed $data; |
17 | public $queue; |
18 | protected RabbitMQService $rabbitMQService; |
19 | |
20 | /** |
21 | * Create a new job instance. |
22 | * |
23 | * @param mixed $data The message data to be published to RabbitMQ. |
24 | * @param string $queue The RabbitMQ queue name. |
25 | * @param RabbitMQService $rabbitMQService The RabbitMQ service instance. |
26 | */ |
27 | public function __construct(mixed $data, string $queue, RabbitMQService $rabbitMQService) |
28 | { |
29 | $this->data = $data; |
30 | $this->queue = $queue; |
31 | $this->rabbitMQService = $rabbitMQService; |
32 | } |
33 | |
34 | /** |
35 | * Execute the job. |
36 | * |
37 | * @return void |
38 | * @throws \Exception |
39 | */ |
40 | public function handle(): void |
41 | { |
42 | // Use the injected RabbitMQService instance |
43 | $this->rabbitMQService->createConnection(true); |
44 | $this->rabbitMQService->publishMessage($this->queue, $this->data); |
45 | $this->rabbitMQService->closeConnection(); |
46 | } |
47 | } |