70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\SourceSyncRun;
|
|
use App\Services\Calendar\CalendarEventSyncService;
|
|
use App\Services\Calendar\CalendarReminderSender;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Carbon;
|
|
use Throwable;
|
|
|
|
class ProcessCalendarRemindersCommand extends Command
|
|
{
|
|
protected $signature = 'calendar:process-reminders
|
|
{--no-sync : Ne synchronise pas l agenda avant traitement}
|
|
{--force-sync : Force la synchronisation CalDAV avant traitement}';
|
|
|
|
protected $description = 'Synchronise l agenda CalDAV et envoie les rappels SMS dus.';
|
|
|
|
public function handle(CalendarEventSyncService $syncService, CalendarReminderSender $sender): int
|
|
{
|
|
if ($this->shouldSync()) {
|
|
try {
|
|
$run = $syncService->sync();
|
|
$this->line((string) $run->message);
|
|
} catch (Throwable $exception) {
|
|
$this->error('Synchronisation CalDAV agenda en echec : '.$exception->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
} elseif (! $this->option('no-sync')) {
|
|
$interval = (int) config('relais_sms.caldav.sync_interval_minutes', 60);
|
|
$this->line('Synchronisation CalDAV ignoree : derniere synchronisation recente, intervalle '.$interval.' minute(s).');
|
|
}
|
|
|
|
$sent = $sender->sendDue();
|
|
$this->info($sent.' rappel(s) SMS envoye(s).');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function shouldSync(): bool
|
|
{
|
|
if ($this->option('no-sync')) {
|
|
return false;
|
|
}
|
|
|
|
if ($this->option('force-sync')) {
|
|
return true;
|
|
}
|
|
|
|
$interval = max(1, (int) config('relais_sms.caldav.sync_interval_minutes', 60));
|
|
$lastRun = SourceSyncRun::query()
|
|
->where('source', 'caldav_calendar')
|
|
->latest('started_at')
|
|
->first();
|
|
|
|
$rawStartedAt = $lastRun?->getRawOriginal('started_at');
|
|
|
|
if ($rawStartedAt === null) {
|
|
return true;
|
|
}
|
|
|
|
$lastStartedAt = Carbon::parse((string) $rawStartedAt, 'UTC');
|
|
$nextAllowedSyncAt = Carbon::now()->utc()->subMinutes($interval);
|
|
|
|
return $lastStartedAt->lessThanOrEqualTo($nextAllowedSyncAt);
|
|
}
|
|
}
|