Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DispatchDealAutomationsJob
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Sales\Infrastructure\Jobs;
6
7use Illuminate\Bus\Queueable;
8use Illuminate\Contracts\Queue\ShouldQueue;
9use Illuminate\Foundation\Bus\Dispatchable;
10use Illuminate\Queue\InteractsWithQueue;
11use Illuminate\Queue\SerializesModels;
12use ImovelZapAi\Sales\Application\Actions\RunDealAutomationsAction;
13use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel;
14use ImovelZapAi\Tenancy\Support\TenantContext;
15
16final class DispatchDealAutomationsJob implements ShouldQueue
17{
18    use Dispatchable;
19    use InteractsWithQueue;
20    use Queueable;
21    use SerializesModels;
22
23    public int $tries = 1;
24
25    public function __construct()
26    {
27        $this->onQueue((string) config('sales.queue', 'sales'));
28    }
29
30    public function handle(RunDealAutomationsAction $action): void
31    {
32        TenantModel::query()
33            ->orderBy('id')
34            ->limit(200)
35            ->get()
36            ->each(function (TenantModel $tenant) use ($action): void {
37                TenantContext::clear();
38                TenantContext::set((string) $tenant->id);
39                try {
40                    $action->execute((string) $tenant->id);
41                } finally {
42                    TenantContext::clear();
43                }
44            });
45    }
46}