<?php
namespace App\Console\Commands;use App\Domain\Transfers\TransferStateMachine;use App\Enums\TransferStatus;use App\Models\Transfer;use Illuminate\Console\Command;
class ExpireOverdueTransfers extends Command {protected $signature='transfers:expire';protected $description='Expire issued transfers past their validity window';public function handle(TransferStateMachine $states):int{Transfer::where('status',TransferStatus::Issued)->where('expires_at','<',now())->chunkById(100,function($rows)use($states){foreach($rows as $t){\DB::transaction(function()use($states,$t){$locked=Transfer::whereKey($t->id)->lockForUpdate()->first();if($locked?->status===TransferStatus::Issued&&$locked->expires_at->isPast())$states->transition($locked,TransferStatus::Expired,null,[],null);});}});return self::SUCCESS;}}
