Certified/app/Models/Certificate.php
Joël van de Wouw ac340d125c
Some checks failed
linter / quality (push) Failing after 13s
tests / ci (8.3) (push) Failing after 2s
tests / ci (8.4) (push) Failing after 2s
tests / ci (8.5) (push) Failing after 3s
Initial commit
Certificate manager built on Laravel 13, Livewire 4, and Flux.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-12 14:44:58 +02:00

64 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ExpiryStatus;
use App\Models\Concerns\OwnedByUser;
use App\Models\Scopes\OwnedByUserScope;
use Database\Factories\CertificateFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* @property int $id
* @property int $user_id
* @property int|null $category_id
* @property string $title
* @property string|null $issuer
* @property string|null $certificate_number
* @property Carbon|null $issued_at
* @property string|null $file_path
* @property Carbon $expires_at
* @property string|null $notes
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
#[Fillable(['title', 'category_id', 'issuer', 'certificate_number', 'issued_at', 'file_path', 'expires_at', 'notes'])]
#[ScopedBy(OwnedByUserScope::class)]
class Certificate extends Model
{
/** @use HasFactory<CertificateFactory> */
use HasFactory, OwnedByUser;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'issued_at' => 'date',
'expires_at' => 'date',
];
}
/**
* @return BelongsTo<Category, $this>
*/
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
public function expiryStatus(int $monthsBefore = 1): ExpiryStatus
{
return match (true) {
$this->expires_at->isPast() && ! $this->expires_at->isToday() => ExpiryStatus::Expired,
$this->expires_at->lte(today()->addMonths($monthsBefore)) => ExpiryStatus::ExpiringSoon,
default => ExpiryStatus::Valid,
};
}
}