fedi-feed-router/app/Models/Article.php

85 lines
2 KiB
PHP
Raw Normal View History

2025-06-29 09:37:49 +02:00
<?php
namespace App\Models;
2025-07-05 18:26:04 +02:00
use App\Events\NewArticleFetched;
2025-06-29 09:37:49 +02:00
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-07-05 18:26:04 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2025-06-30 18:18:30 +02:00
use Illuminate\Database\Eloquent\Relations\HasOne;
2025-06-29 19:46:50 +02:00
use Illuminate\Support\Carbon;
2025-06-29 09:37:49 +02:00
/**
* @method static firstOrCreate(string[] $array)
2025-07-03 21:34:39 +02:00
* @method static where(string $string, string $url)
* @method static create(string[] $array)
2025-06-29 19:46:50 +02:00
* @property integer $id
2025-07-05 18:26:04 +02:00
* @property int $feed_id
* @property Feed $feed
2025-06-29 19:46:50 +02:00
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
2025-06-30 18:18:30 +02:00
* @property ArticlePublication $articlePublication
2025-06-29 09:37:49 +02:00
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
2025-07-05 18:26:04 +02:00
'feed_id',
2025-06-29 09:37:49 +02:00
'url',
2025-06-29 21:20:45 +02:00
'title',
'description',
2025-06-29 19:46:50 +02:00
'is_valid',
2025-06-30 19:54:43 +02:00
'is_duplicate',
2025-06-29 21:20:45 +02:00
'fetched_at',
2025-06-29 19:46:50 +02:00
'validated_at',
2025-06-29 09:37:49 +02:00
];
public function casts(): array
{
return [
2025-06-30 19:54:43 +02:00
'is_valid' => 'boolean',
'is_duplicate' => 'boolean',
2025-06-29 21:20:45 +02:00
'fetched_at' => 'datetime',
2025-06-29 19:46:50 +02:00
'validated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
'created_at' => 'datetime',
2025-06-29 17:13:18 +02:00
'updated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
];
}
2025-06-29 09:48:45 +02:00
2025-06-29 19:46:50 +02:00
public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
2025-06-30 18:18:30 +02:00
public function articlePublication(): HasOne
{
return $this->hasOne(ArticlePublication::class);
}
2025-07-05 18:26:04 +02:00
public function feed(): BelongsTo
2025-06-30 18:18:30 +02:00
{
2025-07-05 18:26:04 +02:00
return $this->belongsTo(Feed::class);
2025-06-30 18:18:30 +02:00
}
2025-06-29 19:46:50 +02:00
protected static function booted(): void
2025-06-29 09:48:45 +02:00
{
static::created(function ($article) {
2025-07-05 18:26:04 +02:00
event(new NewArticleFetched($article));
2025-06-29 09:48:45 +02:00
});
}
2025-06-29 09:37:49 +02:00
}