89 lines
1.8 KiB
PHP
89 lines
1.8 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
class Sitemap extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'site_id',
|
||
|
|
'url',
|
||
|
|
'last_checked_at',
|
||
|
|
'is_valid',
|
||
|
|
'last_error',
|
||
|
|
'is_active',
|
||
|
|
'url_count',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'last_checked_at' => 'datetime',
|
||
|
|
'is_valid' => 'boolean',
|
||
|
|
'is_active' => 'boolean',
|
||
|
|
'url_count' => 'integer',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function site(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Site::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if sitemap has errors
|
||
|
|
*/
|
||
|
|
public function hasErrors(): bool
|
||
|
|
{
|
||
|
|
return !$this->is_valid || !empty($this->last_error);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the status badge color
|
||
|
|
*/
|
||
|
|
public function getStatusBadgeClass(): string
|
||
|
|
{
|
||
|
|
if (!$this->last_checked_at) {
|
||
|
|
return 'bg-secondary';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->is_valid ? 'bg-success' : 'bg-danger';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the status text
|
||
|
|
*/
|
||
|
|
public function getStatusText(): string
|
||
|
|
{
|
||
|
|
if (!$this->last_checked_at) {
|
||
|
|
return 'Not Checked';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->is_valid ? 'Valid' : 'Invalid';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mark sitemap as valid
|
||
|
|
*/
|
||
|
|
public function markAsValid(?int $urlCount = null): void
|
||
|
|
{
|
||
|
|
$this->update([
|
||
|
|
'is_valid' => true,
|
||
|
|
'last_error' => null,
|
||
|
|
'last_checked_at' => now(),
|
||
|
|
'url_count' => $urlCount,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mark sitemap as invalid
|
||
|
|
*/
|
||
|
|
public function markAsInvalid(string $error): void
|
||
|
|
{
|
||
|
|
$this->update([
|
||
|
|
'is_valid' => false,
|
||
|
|
'last_error' => $error,
|
||
|
|
'last_checked_at' => now(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|