Add article publishing events
This commit is contained in:
parent
98bc2b035e
commit
2b05bef5b6
6 changed files with 58 additions and 3 deletions
18
app/Events/ArticleReadyToPublish.php
Normal file
18
app/Events/ArticleReadyToPublish.php
Normal 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 ArticleReadyToPublish
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public function __construct(public Article $article)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -3,8 +3,7 @@
|
|||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ArticleFetched;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Events\ArticleReadyToPublish;
|
||||
|
||||
class CheckArticleKeywords
|
||||
{
|
||||
|
|
@ -15,6 +14,16 @@ public function __construct()
|
|||
|
||||
public function handle(ArticleFetched $event): void
|
||||
{
|
||||
logger('running check article keywords for article ' . $event->article->id);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
app/Listeners/PublishArticle.php
Normal file
19
app/Listeners/PublishArticle.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\ArticleReadyToPublish;
|
||||
|
||||
class PublishArticle
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function handle(ArticleReadyToPublish $event): void
|
||||
{
|
||||
logger('Publishing article: ' . $event->article->id);
|
||||
// TODO: Add actual publishing logic here
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ class Article extends Model
|
|||
|
||||
protected $fillable = [
|
||||
'url',
|
||||
'is_relevant',
|
||||
];
|
||||
|
||||
public function casts(): array
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
namespace App\Providers;
|
||||
|
||||
use App\Events\ArticleFetched;
|
||||
use App\Events\ArticleReadyToPublish;
|
||||
use App\Listeners\CheckArticleKeywords;
|
||||
use App\Listeners\PublishArticle;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
|
|
@ -20,5 +22,10 @@ public function boot(): void
|
|||
ArticleFetched::class,
|
||||
CheckArticleKeywords::class,
|
||||
);
|
||||
|
||||
Event::listen(
|
||||
ArticleReadyToPublish::class,
|
||||
PublishArticle::class,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ public function up(): void
|
|||
Schema::create('articles', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('url');
|
||||
$table->boolean('is_relevant')->nullable();
|
||||
$table->timestamp('created_at')->default(now());
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue