Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.70% |
1 / 27 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CofraiClientsSyncRange | |
3.70% |
1 / 27 |
|
50.00% |
1 / 2 |
65.15 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Services\CofraiClientSyncService; |
| 6 | use Carbon\CarbonPeriod; |
| 7 | use Illuminate\Console\Command; |
| 8 | |
| 9 | /** |
| 10 | * FIRE-1077: Cofrai parallel to `clients:sync-range`. |
| 11 | * Iterates a date range, fetching unique Cofrai client IDs per region |
| 12 | * and upserting them via CofraiClientSyncService. |
| 13 | */ |
| 14 | class CofraiClientsSyncRange extends Command |
| 15 | { |
| 16 | protected $signature = 'cofrai-clients:sync-range {from} {to?} {region?}'; |
| 17 | |
| 18 | protected $description = 'Sync clients modified within a date range from Cofrai. Creates or updates local client records.'; |
| 19 | |
| 20 | public function __construct(private readonly CofraiClientSyncService $syncService) |
| 21 | { |
| 22 | parent::__construct(); |
| 23 | } |
| 24 | |
| 25 | public function handle(): int |
| 26 | { |
| 27 | $from = $this->argument('from'); |
| 28 | $to = $this->argument('to') ?? date('Y-m-d'); |
| 29 | $regions = $this->argument('region') |
| 30 | ? explode(',', $this->argument('region')) |
| 31 | : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'AndalucĂa', 'Baleares']; |
| 32 | |
| 33 | $dates = CarbonPeriod::create($from, $to); |
| 34 | |
| 35 | $this->info("Collecting Cofrai client IDs from {$from} to {$to}..."); |
| 36 | |
| 37 | $hasFailure = false; |
| 38 | $totals = ['created' => 0, 'updated' => 0, 'failed' => 0, 'skipped' => 0]; |
| 39 | |
| 40 | foreach ($regions as $region) { |
| 41 | try { |
| 42 | $this->info("Region {$region}: collecting IDs..."); |
| 43 | |
| 44 | $allIds = []; |
| 45 | foreach ($dates as $date) { |
| 46 | $ids = $this->syncService->fetchClientIds($date->format('Y-m-d'), $region); |
| 47 | $allIds = array_merge($allIds, $ids); |
| 48 | } |
| 49 | |
| 50 | $uniqueIds = array_unique(array_filter($allIds)); |
| 51 | $this->info(" Region {$region}: ".count($allIds).' total IDs, '.count($uniqueIds).' unique. Syncing...'); |
| 52 | |
| 53 | $stats = $this->syncService->syncClientIds($uniqueIds, $region); |
| 54 | |
| 55 | foreach ($stats as $key => $value) { |
| 56 | $totals[$key] += $value; |
| 57 | } |
| 58 | |
| 59 | $this->info(" Region {$region}: created={$stats['created']}, updated={$stats['updated']}, failed={$stats['failed']}, skipped={$stats['skipped']}"); |
| 60 | } catch (\Exception $e) { |
| 61 | $this->error("Region {$region} failed: ".$e->getMessage()); |
| 62 | $hasFailure = true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | $this->info("Totals: created={$totals['created']}, updated={$totals['updated']}, failed={$totals['failed']}, skipped={$totals['skipped']}"); |
| 67 | |
| 68 | return $hasFailure ? Command::FAILURE : Command::SUCCESS; |
| 69 | } |
| 70 | } |