93 lines
2.2 KiB
PHP
93 lines
2.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(array<string, mixed> $array)
|
|
* @method static where(string $string, string $url)
|
|
* @method static create(array<string, mixed> $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',
|
|
];
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @return HasOne<ArticlePublication, $this>
|
|
*/
|
|
public function articlePublication(): HasOne
|
|
{
|
|
return $this->hasOne(ArticlePublication::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Feed, $this>
|
|
*/
|
|
public function feed(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Feed::class);
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::created(function ($article) {
|
|
event(new NewArticleFetched($article));
|
|
});
|
|
}
|
|
}
|