lthn.io/app/Mod/Names/Models/NameClaim.php

63 lines
1.3 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Mod\Names\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Pre-registration name claim.
*
* $claim = NameClaim::create(['name' => 'mybrand', 'email' => 'me@example.com']);
* $pending = NameClaim::pending()->get();
* $claim->approve();
*/
class NameClaim extends Model
{
use \Core\Tenant\Concerns\BelongsToWorkspace;
protected bool $workspaceContextRequired = false;
protected $fillable = [
'claim_id',
'name',
'email',
'status',
'workspace_id',
];
protected $attributes = [
'status' => 'pending',
];
protected static function booted(): void
{
static::creating(function (self $claim) {
if (empty($claim->claim_id)) {
$claim->claim_id = bin2hex(random_bytes(6));
}
});
}
public function scopePending($query)
{
return $query->where('status', 'pending');
}
public function scopeApproved($query)
{
return $query->where('status', 'approved');
}
public function approve(): void
{
$this->update(['status' => 'approved']);
}
public function reject(): void
{
$this->update(['status' => 'rejected']);
}
}