Files
2026-01-17 16:42:43 +02:00

130 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class DnsRecord extends Model
{
protected $fillable = [
'site_id',
'domain',
'record_type',
'expected_value',
'last_checked_at',
'is_valid',
'actual_value',
'error_message',
'is_active',
];
protected $casts = [
'last_checked_at' => 'datetime',
'is_valid' => 'boolean',
'is_active' => 'boolean',
'actual_value' => 'array',
];
/**
* Available DNS record types.
*/
public static function getRecordTypes(): array
{
return [
'A' => 'A (IPv4 Address)',
'AAAA' => 'AAAA (IPv6 Address)',
'CNAME' => 'CNAME (Canonical Name)',
'MX' => 'MX (Mail Exchange)',
'TXT' => 'TXT (Text Record)',
'NS' => 'NS (Name Server)',
'SOA' => 'SOA (Start of Authority)',
'PTR' => 'PTR (Pointer)',
'SRV' => 'SRV (Service)',
];
}
/**
* Get the site that owns the DNS record.
*/
public function site(): BelongsTo
{
return $this->belongsTo(Site::class);
}
/**
* Scope to filter active DNS records.
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Scope to filter valid records.
*/
public function scopeValid($query)
{
return $query->where('is_valid', true);
}
/**
* Scope to filter invalid records.
*/
public function scopeInvalid($query)
{
return $query->where('is_valid', false);
}
/**
* Get status badge color.
*/
public function getStatusColorAttribute(): string
{
if ($this->is_valid === null) {
return 'secondary';
}
return $this->is_valid ? 'success' : 'danger';
}
/**
* Get status text.
*/
public function getStatusTextAttribute(): string
{
if ($this->is_valid === null) {
return 'Unknown';
}
return $this->is_valid ? 'Valid' : 'Invalid';
}
/**
* Get display name for the record.
*/
public function getDisplayNameAttribute(): string
{
return "{$this->record_type} - {$this->domain}";
}
/**
* Get formatted actual value for display.
*/
public function getFormattedActualValueAttribute(): ?string
{
if (!$this->actual_value) {
return null;
}
if (is_array($this->actual_value)) {
if (count($this->actual_value) === 1) {
return $this->actual_value[0];
}
return implode(', ', $this->actual_value);
}
return $this->actual_value;
}
}