2026-03-18 15:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-03-18 15:46:15 +01:00
|
|
|
use App\Enums\ApprovalStatusEnum;
|
2026-03-18 17:31:47 +01:00
|
|
|
use App\Enums\PublishStatusEnum;
|
2026-03-18 17:00:56 +01:00
|
|
|
use App\Events\RouteArticleApproved;
|
2026-03-18 15:24:03 +01:00
|
|
|
use Database\Factories\RouteArticleFactory;
|
|
|
|
|
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 $feed_id
|
|
|
|
|
* @property int $platform_channel_id
|
|
|
|
|
* @property int $article_id
|
2026-03-18 15:46:15 +01:00
|
|
|
* @property ApprovalStatusEnum $approval_status
|
2026-03-18 17:31:47 +01:00
|
|
|
* @property PublishStatusEnum $publish_status
|
2026-08-10 00:05:17 +02:00
|
|
|
* @property int $publish_attempts
|
|
|
|
|
* @property Carbon|null $next_attempt_at
|
2026-03-18 15:24:03 +01:00
|
|
|
* @property Carbon|null $validated_at
|
|
|
|
|
* @property Carbon $created_at
|
|
|
|
|
* @property Carbon $updated_at
|
|
|
|
|
*/
|
|
|
|
|
class RouteArticle extends Model
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<RouteArticleFactory> */
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'feed_id',
|
|
|
|
|
'platform_channel_id',
|
|
|
|
|
'article_id',
|
|
|
|
|
'approval_status',
|
2026-03-18 17:31:47 +01:00
|
|
|
'publish_status',
|
2026-08-10 00:05:17 +02:00
|
|
|
'publish_attempts',
|
|
|
|
|
'next_attempt_at',
|
2026-03-18 15:24:03 +01:00
|
|
|
'validated_at',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
2026-03-18 15:46:15 +01:00
|
|
|
'approval_status' => ApprovalStatusEnum::class,
|
2026-03-18 17:31:47 +01:00
|
|
|
'publish_status' => PublishStatusEnum::class,
|
2026-08-10 00:05:17 +02:00
|
|
|
'publish_attempts' => 'integer',
|
|
|
|
|
'next_attempt_at' => 'datetime',
|
2026-03-18 15:24:03 +01:00
|
|
|
'validated_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return BelongsTo<Route, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function route(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Route::class, 'feed_id', 'feed_id')
|
|
|
|
|
->where('platform_channel_id', $this->platform_channel_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return BelongsTo<Article, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function article(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Article::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return BelongsTo<Feed, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function feed(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Feed::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return BelongsTo<PlatformChannel, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function platformChannel(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(PlatformChannel::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isPending(): bool
|
|
|
|
|
{
|
2026-03-18 15:46:15 +01:00
|
|
|
return $this->approval_status === ApprovalStatusEnum::PENDING;
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isApproved(): bool
|
|
|
|
|
{
|
2026-03-18 15:46:15 +01:00
|
|
|
return $this->approval_status === ApprovalStatusEnum::APPROVED;
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isRejected(): bool
|
|
|
|
|
{
|
2026-03-18 15:46:15 +01:00
|
|
|
return $this->approval_status === ApprovalStatusEnum::REJECTED;
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function approve(): void
|
|
|
|
|
{
|
2026-08-02 15:45:26 +02:00
|
|
|
if ($this->isApproved()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 15:46:15 +01:00
|
|
|
$this->update(['approval_status' => ApprovalStatusEnum::APPROVED]);
|
2026-03-18 17:00:56 +01:00
|
|
|
|
|
|
|
|
event(new RouteArticleApproved($this));
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function reject(): void
|
|
|
|
|
{
|
2026-03-18 15:46:15 +01:00
|
|
|
$this->update(['approval_status' => ApprovalStatusEnum::REJECTED]);
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|
2026-08-10 00:05:17 +02:00
|
|
|
|
|
|
|
|
private const RETRY_BACKOFF_MINUTES = [5, 30, 120, 360];
|
|
|
|
|
|
|
|
|
|
public const MAX_PUBLISH_ATTEMPTS = 4;
|
|
|
|
|
|
|
|
|
|
public function recordPublishAttemptFailed(): void
|
|
|
|
|
{
|
|
|
|
|
$attempts = $this->publish_attempts + 1;
|
|
|
|
|
$backoff = self::RETRY_BACKOFF_MINUTES;
|
|
|
|
|
|
|
|
|
|
$this->update([
|
|
|
|
|
'publish_attempts' => $attempts,
|
|
|
|
|
'next_attempt_at' => now()->addMinutes($backoff[$attempts - 1] ?? end($backoff)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function clearPublishAttempts(): void
|
|
|
|
|
{
|
|
|
|
|
$this->update(['publish_attempts' => 0, 'next_attempt_at' => null]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasExhaustedPublishAttempts(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->publish_attempts >= self::MAX_PUBLISH_ATTEMPTS;
|
|
|
|
|
}
|
2026-03-18 15:24:03 +01:00
|
|
|
}
|