29 lines
1.0 KiB
PHP
29 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Models\Channel;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class RotateChannelToken
|
|
{
|
|
public function handle(Channel $channel, int $graceHours = 168): Channel
|
|
{
|
|
return DB::transaction(function () use ($channel, $graceHours): Channel {
|
|
$locked = Channel::query()->whereKey($channel->id)->lockForUpdate()->firstOrFail();
|
|
if ($graceHours < 24 || $graceHours > 720 || $locked->previous_export_token_expires_at?->isFuture()) {
|
|
throw ValidationException::withMessages(['grace_hours' => '24 bis 720 Stunden wählen; eine laufende Token-Übergangsphase muss zuerst ablaufen.']);
|
|
}
|
|
$locked->forceFill([
|
|
'previous_export_token' => $locked->export_token,
|
|
'previous_export_token_expires_at' => now()->addHours($graceHours),
|
|
'export_token' => Str::random(48),
|
|
])->save();
|
|
|
|
return $locked->refresh();
|
|
});
|
|
}
|
|
}
|