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

81 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Mod\Names\Models;
use Illuminate\Database\Eloquent\Model;
/**
* DNS change ticket tracks alias update transactions.
*
* $ticket = DnsTicket::open('mybrand', $records, $txId);
* $ticket->confirm();
* $pending = DnsTicket::pending()->get();
*/
class DnsTicket extends Model
{
use \Core\Tenant\Concerns\BelongsToWorkspace;
protected bool $workspaceContextRequired = false;
protected $fillable = [
'ticket_id',
'name',
'status',
'tx_id',
'records',
'address',
'workspace_id',
'comment',
];
protected $casts = [
'records' => 'array',
];
protected static function booted(): void
{
static::creating(function (self $ticket) {
if (empty($ticket->ticket_id)) {
$ticket->ticket_id = bin2hex(random_bytes(6));
}
});
}
public function scopePending($query)
{
return $query->where('status', 'pending');
}
public function scopeQueued($query)
{
return $query->where('status', 'queued');
}
public function confirm(): void
{
$this->update(['status' => 'confirmed']);
}
public function fail(): void
{
$this->update(['status' => 'failed']);
}
/**
* Open a new DNS change ticket.
*/
public static function open(string $name, array $records, ?string $txId = null, ?string $address = null, ?string $comment = null): self
{
return static::create([
'name' => $name,
'status' => $txId ? 'pending' : 'queued',
'tx_id' => $txId,
'records' => $records,
'address' => $address,
'comment' => $comment,
]);
}
}