Chain checking of keywords

This commit is contained in:
myrmidex 2025-06-29 09:48:45 +02:00
parent f141ab7889
commit 98bc2b035e
4 changed files with 53 additions and 7 deletions

View file

@ -0,0 +1,18 @@
<?php
namespace App\Events;
use App\Models\Article;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ArticleFetched
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(public Article $article)
{
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace App\Listeners;
use App\Events\ArticleFetched;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class CheckArticleKeywords
{
public function __construct()
{
//
}
public function handle(ArticleFetched $event): void
{
logger('running check article keywords for article ' . $event->article->id);
}
}

View file

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Events\ArticleFetched;
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;
@ -24,4 +25,11 @@ public function casts(): array
'created_at' => 'datetime', 'created_at' => 'datetime',
]; ];
} }
protected static function booted()
{
static::created(function ($article) {
event(new ArticleFetched($article));
});
}
} }

View file

@ -2,23 +2,23 @@
namespace App\Providers; namespace App\Providers;
use App\Events\ArticleFetched;
use App\Listeners\CheckArticleKeywords;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
/**
* Register any application services.
*/
public function register(): void public function register(): void
{ {
// //
} }
/**
* Bootstrap any application services.
*/
public function boot(): void public function boot(): void
{ {
// Event::listen(
ArticleFetched::class,
CheckArticleKeywords::class,
);
} }
} }