fedi-feed-router/app/Models/Article.php
2025-07-05 18:26:04 +02:00

84 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Events\NewArticleFetched;
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
/**
* @method static firstOrCreate(string[] $array)
* @method static where(string $string, string $url)
* @method static create(string[] $array)
* @property integer $id
* @property int $feed_id
* @property Feed $feed
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property ArticlePublication $articlePublication
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
'feed_id',
'url',
'title',
'description',
'is_valid',
'is_duplicate',
'fetched_at',
'validated_at',
];
public function casts(): array
{
return [
'is_valid' => 'boolean',
'is_duplicate' => 'boolean',
'fetched_at' => 'datetime',
'validated_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}
public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
public function articlePublication(): HasOne
{
return $this->hasOne(ArticlePublication::class);
}
public function feed(): BelongsTo
{
return $this->belongsTo(Feed::class);
}
protected static function booted(): void
{
static::created(function ($article) {
event(new NewArticleFetched($article));
});
}
}