Page MenuHomePhorge

SignupCodeObserver.php
No OneTemporary

Size
994 B
Referenced Files
None
Subscribers
None

SignupCodeObserver.php

<?php
namespace App\Observers;
use App\SignupCode;
use Carbon\Carbon;
use Illuminate\Support\Str;
class SignupCodeObserver
{
/**
* Handle the "creating" event.
*
* Ensure that the code entry is created with a random code/short_code.
*
* @param \App\SignupCode $code The code being created.
*
* @return void
*/
public function creating(SignupCode $code): void
{
$code_length = SignupCode::CODE_LENGTH;
$exp_hours = env('SIGNUP_CODE_EXPIRY', SignupCode::CODE_EXP_HOURS);
if (empty($code->code)) {
$code->short_code = SignupCode::generateShortCode();
// FIXME: Replace this with something race-condition free
while (true) {
$code->code = Str::random($code_length);
if (!SignupCode::find($code->code)) {
break;
}
}
}
$code->expires_at = Carbon::now()->addHours($exp_hours);
}
}

File Metadata

Mime Type
text/x-php
Expires
Thu, Jul 30, 3:05 AM (14 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1172524
Default Alt Text
SignupCodeObserver.php (994 B)

Event Timeline