Changeset View
Changeset View
Standalone View
Standalone View
src/app/VerificationCode.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\SignupCode; | use Carbon\Carbon; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
/** | /** | ||||
* The eloquent definition of a VerificationCode | * The eloquent definition of a VerificationCode | ||||
* | * | ||||
* @property string $code | |||||
* @property string $mode | * @property string $mode | ||||
* @property \App\User $user | * @property \App\User $user | ||||
* @property int $user_id | |||||
* @property string $short_code | |||||
*/ | */ | ||||
class VerificationCode extends SignupCode | class VerificationCode extends Model | ||||
{ | { | ||||
// Code expires after so many hours | // Code expires after so many hours | ||||
public const CODE_EXP_HOURS = 8; | |||||
public const SHORTCODE_LENGTH = 8; | public const SHORTCODE_LENGTH = 8; | ||||
public const CODE_LENGTH = 32; | |||||
// Code expires after so many hours | |||||
public const CODE_EXP_HOURS = 8; | |||||
/** | /** | ||||
* The attributes that are mass assignable. | * The primary key associated with the table. | ||||
* | * | ||||
* @var array | * @var string | ||||
*/ | */ | ||||
protected $fillable = ['user_id', 'code', 'short_code', 'mode', 'expires_at']; | protected $primaryKey = 'code'; | ||||
/** | |||||
* Indicates if the IDs are auto-incrementing. | |||||
* | |||||
* @var bool | |||||
*/ | |||||
public $incrementing = false; | |||||
/** | /** | ||||
* The attributes that should be cast to native types. | * The "type" of the auto-incrementing ID. | ||||
* | |||||
* @var string | |||||
*/ | |||||
protected $keyType = 'string'; | |||||
/** | |||||
* Indicates if the model should be timestamped. | |||||
* | |||||
* @var bool | |||||
*/ | |||||
public $timestamps = false; | |||||
/** | |||||
* The attributes that are mass assignable. | |||||
* | * | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $casts = []; | protected $fillable = ['user_id', 'code', 'short_code', 'mode', 'expires_at']; | ||||
/** | /** | ||||
* The user to which this setting belongs. | * The attributes that should be mutated to dates. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | * @var array | ||||
*/ | */ | ||||
public function user() | protected $dates = ['expires_at']; | ||||
{ | |||||
return $this->belongsTo('\App\User', 'user_id', 'id'); | |||||
} | |||||
/** | /** | ||||
* Generate a short code (for human). | * Generate a short code (for human). | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public static function generateShortCode(): string | public static function generateShortCode(): string | ||||
{ | { | ||||
$code_length = env('VERIFICATION_CODE_LENGTH', self::SHORTCODE_LENGTH); | $code_length = env('VERIFICATION_CODE_LENGTH', self::SHORTCODE_LENGTH); | ||||
return \App\Utils::randStr($code_length); | return \App\Utils::randStr($code_length); | ||||
} | } | ||||
/** | |||||
* Check if code is expired. | |||||
* | |||||
* @return bool True if code is expired, False otherwise | |||||
*/ | |||||
public function isExpired() | |||||
{ | |||||
// @phpstan-ignore-next-line | |||||
return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false; | |||||
} | |||||
/** | |||||
* The user to which this setting belongs. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |||||
*/ | |||||
public function user() | |||||
{ | |||||
return $this->belongsTo('\App\User', 'user_id', 'id'); | |||||
} | |||||
} | } |