Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IfexQuotationsSync
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
6
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%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\IfexController;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8use Symfony\Component\HttpFoundation\Response;
9
10class IfexQuotationsSync extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'ifex-quotations:sync
18        {--from= : Start of the window (fechainicio), Y-m-d. Defaults to today}
19        {--to= : End of the window (fechafin), Y-m-d. Defaults to today}';
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Synchronizes IFEX budgets created or edited in a date window (defaults to today)';
27
28    /**
29     * Create a new command instance.
30     */
31    public function __construct(protected IfexController $ifexController)
32    {
33        parent::__construct();
34    }
35
36    /**
37     * Execute the console command.
38     */
39    public function handle(): int
40    {
41        try {
42            $result = $this->ifexController->syncQuotations(
43                $this->option('from'),
44                $this->option('to'),
45            );
46
47            // syncQuotations() catches its own failures and returns a 500 JSON
48            // response instead of throwing, so the catch below never sees them.
49            // Without this the command exited 0 on a failed sync — including a
50            // rejected --from/--to — which is invisible to cron and to anyone
51            // scripting it.
52            if ($result instanceof Response) {
53                $payload = json_decode((string) $result->getContent(), true);
54                $error = is_array($payload) ? ($payload['error'] ?? null) : null;
55
56                $this->error('IFEX sync failed: '.($error ?? 'unknown error'));
57                Log::channel('g3w')->error('Synchronization failed for ifex update, Error: '.($error ?? 'unknown error'));
58
59                return Command::FAILURE;
60            }
61
62            $count = is_array($result) ? count((array) ($result['quotations'] ?? [])) : 0;
63            $this->info("IFEX sync completed · {$count} budget(s) processed.");
64
65            return Command::SUCCESS;
66        } catch (\Throwable $e) {
67            // FIRE-1258: Throwable (not Exception) so PHP 8 Errors (TypeError
68            // etc.) surface in the log instead of bubbling out unhandled.
69            Log::channel('g3w')->error('Synchronization failed for ifex update, Error: '.$e->getMessage());
70
71            return Command::FAILURE;
72        }
73    }
74}