Page MenuHomePhorge

VerificationCodeObserver.php
No OneTemporary

Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None

VerificationCodeObserver.php

<?php
namespace App\Observers;
use App\VerificationCode;
use Carbon\Carbon;
use Illuminate\Support\Str;
class VerificationCodeObserver
{
/**
* Handle the "creating" event.
*
* Ensure that the code entry is created with a random code/short_code.
*
* @param \App\VerificationCode $code The code being created.
*
* @return void
*/
public function creating(VerificationCode $code): void
{
$code_length = VerificationCode::CODE_LENGTH;
$exp_hours = env('VERIFICATION_CODE_EXPIRY', VerificationCode::CODE_EXP_HOURS);
if (empty($code->code)) {
$code->short_code = VerificationCode::generateShortCode();
// FIXME: Replace this with something race-condition free
while (true) {
$code->code = Str::random($code_length);
if (!VerificationCode::find($code->code)) {
break;
}
}
}
if (empty($code->expires_at)) {
$code->expires_at = Carbon::now()->addHours($exp_hours);
}
// Verification codes are active by default
// Note: This is not required, but this way we make sure the property value
// is a boolean not null after create() call, if it wasn't specified there.
if (!isset($code->active)) {
$code->active = true;
}
}
}

File Metadata

Mime Type
text/x-php
Expires
Sat, Apr 4, 3:59 AM (19 h, 54 m ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
9e/28/7b378e0489cf93a7c336de0f0d43
Default Alt Text
VerificationCodeObserver.php (1 KB)

Event Timeline