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
BulkSyncCofraiRunCommand
5.26% covered (danger)
5.26%
1 / 19
50.00% covered (danger)
50.00%
1 / 2
5.40
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
2
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\CofraiBulkSyncService;
6use Illuminate\Console\Command;
7
8/**
9 * Cofrai mirror of BulkSyncRun (FIRE-1170): CLI wrapper around
10 * `CofraiQuotationsService::bulkSyncByFilters` so the `bulkSync` HTTP
11 * controller can fork it into a background process via exec().
12 *
13 * This is the same pattern `CofraiBudgetsController::syncByDate` uses for
14 * the daily cron sync — `QUEUE_CONNECTION=sync` would otherwise block the
15 * HTTP POST for 20+ minutes on a real run, and `dispatchAfterResponse`
16 * doesn't actually flush the response early under Apache mod_php.
17 *
18 * Invocation contract:
19 *   php artisan cofrai-bulk-sync:run {runId} {region} {dateFrom} {dateTo}
20 *   {fields=client,amount,...} {suppressOps=0|1} {suppressFin=0|1}
21 */
22class BulkSyncCofraiRunCommand extends Command
23{
24    protected $signature = 'cofrai-bulk-sync:run
25        {runId : tbl_cofrai_bulk_sync_runs.id}
26        {region : Region name (matches tbl_companies.region)}
27        {dateFrom : YYYY-MM-DD}
28        {dateTo : YYYY-MM-DD}
29        {fields : Comma-separated UI field names}
30        {suppressOps=0 : 1 if ops approval emails should be suppressed}
31        {suppressFin=0 : 1 if financial approval cascade should be suppressed}';
32
33    protected $description = 'Cofrai bulk-sync worker — invoked by the controller via exec()';
34
35    public function __construct(private readonly CofraiBulkSyncService $cofraiBulkSync)
36    {
37        parent::__construct();
38    }
39
40    public function handle(): int
41    {
42        // The job can run for 20+ minutes. We're already in a CLI context so
43        // PHP's default time limit is 0, but be explicit in case php.ini was
44        // overridden in this image.
45        @set_time_limit(0);
46
47        $runId = (int) $this->argument('runId');
48        $region = (string) $this->argument('region');
49        $dateFrom = (string) $this->argument('dateFrom');
50        $dateTo = (string) $this->argument('dateTo');
51        $fields = array_values(array_filter(array_map('trim', explode(',', (string) $this->argument('fields')))));
52        $suppressOps = (bool) (int) $this->argument('suppressOps');
53        $suppressFin = (bool) (int) $this->argument('suppressFin');
54
55        $this->cofraiBulkSync->bulkSyncByFilters(
56            $runId,
57            $region,
58            $dateFrom,
59            $dateTo,
60            $fields,
61            $suppressOps,
62            $suppressFin,
63        );
64
65        return self::SUCCESS;
66    }
67}