Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.26% covered (danger)
5.26%
1 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CofraiQuotationsSyncMissing
5.26% covered (danger)
5.26%
1 / 19
50.00% covered (danger)
50.00%
1 / 2
48.66
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 / 18
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\CofraiController;
6use Illuminate\Console\Command;
7use Illuminate\Http\Request;
8use Illuminate\Support\Facades\Log;
9
10/**
11 * FIRE-1077: Cofrai parallel to `quotations:sync-missing`.
12 * Iterates configured regions and calls
13 * CofraiController::syncAllBudgetMonitor for each, which pulls the
14 * missing IDs from Cofrai and syncs them locally.
15 */
16class CofraiQuotationsSyncMissing extends Command
17{
18    protected $signature = 'cofrai-quotations:sync-missing {region?} {day?}';
19
20    protected $description = 'Synchronizes missing Cofrai-sourced quotations';
21
22    public function __construct(protected CofraiController $cofraiController)
23    {
24        parent::__construct();
25    }
26
27    public function handle(): int
28    {
29        $regions = $this->argument('region')
30            ? explode(',', $this->argument('region'))
31            : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'Andalucía', 'Baleares', 'Castellón'];
32
33        $day = $this->argument('day') ?: now()->subDay()->format('Y-m-d');
34
35        $this->info("Starting Cofrai synchronization for day: {$day}");
36
37        $hasFailure = false;
38
39        foreach ($regions as $region) {
40            try {
41                $this->info("Synchronizing missing Cofrai quotations for region: {$region}");
42
43                $request = Request::create('/', 'GET', ['day' => $day]);
44                $request->server->set('HTTP_REGION', $region);
45                $request->headers->set('Region', $region);
46
47                $this->cofraiController->syncAllBudgetMonitor($request);
48
49                $this->info("Completed for region: {$region}");
50            } catch (\Exception $e) {
51                Log::channel('cofrai')->error("Cofrai synchronization failed for region: $region, Error: ".$e->getMessage());
52                $hasFailure = true;
53            }
54        }
55
56        $this->info('All regions processed!');
57
58        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
59    }
60}