Certified/app/Models/User.php
2026-08-09 16:33:53 +02:00

125 lines
3.5 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\SubscriptionPlan;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\PasskeyUser;
use Laravel\Fortify\PasskeyAuthenticatable;
use Laravel\Fortify\TwoFactorAuthenticatable;
/**
* @property int $id
* @property string $name
* @property string $email
* @property Carbon|null $email_verified_at
* @property string $password
* @property string|null $two_factor_secret
* @property string|null $two_factor_recovery_codes
* @property Carbon|null $two_factor_confirmed_at
* @property string|null $remember_token
* @property int $reminder_months_before
* @property string $ocr_provider
* @property string|null $ocr_api_key
* @property string $locale
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token', 'ocr_api_key'])]
class User extends Authenticatable implements PasskeyUser
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable;
/**
* @var array<string, mixed>
*/
protected $attributes = [
'reminder_months_before' => 1,
'ocr_provider' => 'none',
'locale' => 'nl',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'reminder_months_before' => 'integer',
'ocr_api_key' => 'encrypted',
];
}
/**
* @return HasMany<Certificate, $this>
*/
public function certificates(): HasMany
{
return $this->hasMany(Certificate::class);
}
/**
* @return HasMany<Category, $this>
*/
public function categories(): HasMany
{
return $this->hasMany(Category::class);
}
/**
* @return HasOne<Subscription, $this>
*/
public function subscription(): HasOne
{
return $this->hasOne(Subscription::class);
}
/**
* The user's current plan. Falls back to Free for a user without a
* subscription row yet (e.g. factory-created users in tests) rather
* than requiring every call site to null-check.
*/
public function plan(): SubscriptionPlan
{
return $this->subscription?->plan ?? SubscriptionPlan::Free;
}
/**
* Whether the user has already reached their plan's certificate limit,
* so a new upload should be blocked. Always false for unlimited plans.
*/
public function certificateLimitReached(): bool
{
$limit = $this->plan()->certificateLimit();
return $limit !== null && $this->certificates()->count() >= $limit;
}
/**
* Get the user's initials
*/
public function initials(): string
{
$initials = Str::initials($this->name, true);
return Str::length($initials) > 1
? Str::substr($initials, 0, 1).Str::substr($initials, -1)
: $initials;
}
}