Adapt validation

This commit is contained in:
myrmidex 2025-06-29 19:46:50 +02:00
parent dcb50f53b5
commit d341c616f0
6 changed files with 76 additions and 35 deletions

View file

@ -1,30 +0,0 @@
<?php
namespace App\Listeners;
use App\Events\ArticleFetched;
use App\Events\ArticleReadyToPublish;
class CheckArticleKeywords
{
public function __construct()
{
//
}
public function handle(ArticleFetched $event): void
{
logger('Checking keywords for article: ' . $event->article->id);
// TODO: Add actual keyword checking logic here
// For now, mark all articles as relevant
$isRelevant = true;
$event->article->update(['is_relevant' => $isRelevant]);
if ($isRelevant) {
event(new ArticleReadyToPublish($event->article));
}
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Listeners;
use App\Events\ArticleFetched;
use App\Events\ArticleReadyToPublish;
use App\Services\Article\ValidationService;
class ValidateArticle
{
public function __construct()
{}
public function handle(ArticleFetched $event): void
{
$article = $event->article;
if (! is_null($article->validated_at)) {
return;
}
$article = ValidationService::validate($article);
if ($article->isValid()) {
event(new ArticleReadyToPublish($event->article));
}
}
}

View file

@ -6,9 +6,16 @@
use Database\Factories\ArticleFactory; use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/** /**
* @method static firstOrCreate(string[] $array) * @method static firstOrCreate(string[] $array)
* @property integer $id
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
*/ */
class Article extends Model class Article extends Model
{ {
@ -17,18 +24,33 @@ class Article extends Model
protected $fillable = [ protected $fillable = [
'url', 'url',
'is_relevant', 'is_valid',
'validated_at',
]; ];
public function casts(): array public function casts(): array
{ {
return [ return [
'validated_at' => 'datetime',
'created_at' => 'datetime', 'created_at' => 'datetime',
'updated_at' => 'datetime', 'updated_at' => 'datetime',
]; ];
} }
protected static function booted() public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
protected static function booted(): void
{ {
static::created(function ($article) { static::created(function ($article) {
event(new ArticleFetched($article)); event(new ArticleFetched($article));

View file

@ -5,7 +5,7 @@
use App\Events\ArticleFetched; use App\Events\ArticleFetched;
use App\Events\ArticleReadyToPublish; use App\Events\ArticleReadyToPublish;
use App\Events\ExceptionOccurred; use App\Events\ExceptionOccurred;
use App\Listeners\CheckArticleKeywords; use App\Listeners\ValidateArticle;
use App\Listeners\LogExceptionToDatabase; use App\Listeners\LogExceptionToDatabase;
use App\Listeners\PublishArticle; use App\Listeners\PublishArticle;
use App\LogLevelEnum; use App\LogLevelEnum;
@ -25,7 +25,7 @@ public function boot(): void
{ {
Event::listen( Event::listen(
ArticleFetched::class, ArticleFetched::class,
CheckArticleKeywords::class, ValidateArticle::class,
); );
Event::listen( Event::listen(

View file

@ -0,0 +1,20 @@
<?php
namespace App\Services\Article;
use App\Models\Article;
class ValidationService
{
public static function validate(Article $article): Article
{
logger('Checking keywords for article: ' . $article->id);
$article->update([
'is_valid' => true,
'validated_at' => now(),
]);
return $article->refresh();
}
}

View file

@ -11,7 +11,8 @@ public function up(): void
Schema::create('articles', function (Blueprint $table) { Schema::create('articles', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('url'); $table->string('url');
$table->boolean('is_relevant')->nullable(); $table->boolean('is_valid')->nullable();
$table->timestamp('validated_at')->nullable();
$table->timestamps(); $table->timestamps();
}); });
} }