Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotationsSyncMissing
83.33% covered (warning)
83.33%
15 / 18
50.00% covered (danger)
50.00%
1 / 2
6.17
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
5.14
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\GestionaController;
6use Illuminate\Console\Command;
7use Illuminate\Http\Request;
8use Illuminate\Support\Facades\Log;
9
10class QuotationsSyncMissing extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'quotations:sync-missing {region?} {day?}';
18
19    /**
20     * The console command description.
21     *
22     * @var string
23     */
24    protected $description = 'Synchronizes missing quotations';
25
26    /**
27     * Create a new command instance.
28     */
29    public function __construct(protected GestionaController $gestionaController)
30    {
31        parent::__construct();
32    }
33
34    /**
35     * Execute the console command.
36     */
37    public function handle(): int
38    {
39        // Same regions as the main sync: one list, so none is left out here.
40        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : QuotationsSync::DEFAULT_REGIONS;
41        $day = now()->subDay()->format('Y-m-d');
42
43        $this->info("Starting synchronization for day: {$day}");
44
45        $hasFailure = false;
46
47        foreach ($regions as $region) {
48            try {
49                $this->info("Synchronizing missing quotations for region: {$region}");
50
51                $request = Request::create('/', 'GET', ['day' => $day]);
52                $request->server->set('HTTP_REGION', $region);
53                $request->headers->set('Region', $region);
54
55                $this->info("Sending region: {$region}");
56
57                $response = $this->gestionaController->syncAllBudgetMonitor($request);
58
59                $this->info("Completed for region: {$region}");
60            } catch (\Exception $e) {
61                Log::channel('g3w')->error("Synchronization failed for region: $region, Error: ".$e->getMessage());
62                $hasFailure = true;
63            }
64        }
65
66        $this->info('All regions processed!');
67
68        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
69    }
70}