Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.56% covered (danger)
5.56%
1 / 18
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotationsSyncMissing
5.56% covered (danger)
5.56%
1 / 18
50.00% covered (danger)
50.00%
1 / 2
36.33
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
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
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        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'AndalucĂ­a', 'Baleares'];
40        $day = now()->subDay()->format('Y-m-d');
41
42        $this->info("Starting synchronization for day: {$day}");
43
44        $hasFailure = false;
45
46        foreach ($regions as $region) {
47            try {
48                $this->info("Synchronizing missing quotations for region: {$region}");
49
50                $request = Request::create('/', 'GET', ['day' => $day]);
51                $request->server->set('HTTP_REGION', $region);
52                $request->headers->set('Region', $region);
53
54                $this->info("Sending region: {$region}");
55
56                $response = $this->gestionaController->syncAllBudgetMonitor($request);
57
58                $this->info("Completed for region: {$region}");
59            } catch (\Exception $e) {
60                Log::channel('g3w')->error("Synchronization failed for region: $region, Error: ".$e->getMessage());
61                $hasFailure = true;
62            }
63        }
64
65        $this->info('All regions processed!');
66
67        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
68    }
69}