Adapt validation
This commit is contained in:
parent
dcb50f53b5
commit
d341c616f0
6 changed files with 76 additions and 35 deletions
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
28
app/Listeners/ValidateArticle.php
Normal file
28
app/Listeners/ValidateArticle.php
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,16 @@
|
|||
use Database\Factories\ArticleFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @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
|
||||
{
|
||||
|
|
@ -17,18 +24,33 @@ class Article extends Model
|
|||
|
||||
protected $fillable = [
|
||||
'url',
|
||||
'is_relevant',
|
||||
'is_valid',
|
||||
'validated_at',
|
||||
];
|
||||
|
||||
public function casts(): array
|
||||
{
|
||||
return [
|
||||
'validated_at' => 'datetime',
|
||||
'created_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) {
|
||||
event(new ArticleFetched($article));
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
use App\Events\ArticleFetched;
|
||||
use App\Events\ArticleReadyToPublish;
|
||||
use App\Events\ExceptionOccurred;
|
||||
use App\Listeners\CheckArticleKeywords;
|
||||
use App\Listeners\ValidateArticle;
|
||||
use App\Listeners\LogExceptionToDatabase;
|
||||
use App\Listeners\PublishArticle;
|
||||
use App\LogLevelEnum;
|
||||
|
|
@ -25,7 +25,7 @@ public function boot(): void
|
|||
{
|
||||
Event::listen(
|
||||
ArticleFetched::class,
|
||||
CheckArticleKeywords::class,
|
||||
ValidateArticle::class,
|
||||
);
|
||||
|
||||
Event::listen(
|
||||
|
|
|
|||
20
app/Services/Article/ValidationService.php
Normal file
20
app/Services/Article/ValidationService.php
Normal 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();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,8 @@ public function up(): void
|
|||
Schema::create('articles', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('url');
|
||||
$table->boolean('is_relevant')->nullable();
|
||||
$table->boolean('is_valid')->nullable();
|
||||
$table->timestamp('validated_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue