Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DispatchOwnerAlertsJob
100.00% covered (success)
100.00%
7 / 7
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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Owners\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\Owners\Application\Actions\RunOwnerAlertsAction;
13use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel;
14use ImovelZapAi\Tenancy\Support\TenantContext;
15
16final class DispatchOwnerAlertsJob 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('owners.queue', 'owners'));
28    }
29
30    public function handle(RunOwnerAlertsAction $action): void
31    {
32        TenantModel::query()->orderBy('id')->limit(200)->get()->each(function (TenantModel $tenant) use ($action): void {
33            TenantContext::clear();
34            TenantContext::set((string) $tenant->id);
35            try {
36                $action->execute((string) $tenant->id);
37            } finally {
38                TenantContext::clear();
39            }
40        });
41    }
42}