64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Mail\NewArticlePublished;
|
|
use App\Models\Article;
|
|
use App\Models\Subscriber;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class QueueNewArticleAlertForSubscribers extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'app:queue-new-article-alert-for-subscribers';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Gets all the published articles that have yet to have subscribers notified and queues notification emails for all the subscribers in the list.';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$startTime = microtime(true);
|
|
|
|
// TODO: Incredibly basic implementation here. Won't scale well past a few thousand subscribers
|
|
$articlesForNotification = Article::query()
|
|
->where('published_at', '<', now())
|
|
->whereNull('subscribers_notified_at')
|
|
->get();
|
|
|
|
logger(count($articlesForNotification) . ' article(s) ready for notifying.');
|
|
|
|
// HARD CODE LIMIT 100 subscribers. (Kinda scared of gettings spammed and want to monitor it slowly)
|
|
$subscribersToNotify = Subscriber::limit(100)->get();
|
|
|
|
logger(count($subscribersToNotify) . ' subscribers(s) to notify.');
|
|
|
|
logger((count($articlesForNotification) * count($subscribersToNotify)) . ' emails being queued for send.');
|
|
|
|
foreach($articlesForNotification as $article) {
|
|
|
|
$article->update(['subscribers_notified_at' => now()]);
|
|
|
|
foreach($subscribersToNotify as $subscriber) {
|
|
Mail::to($subscriber)->queue(new NewArticlePublished($article, $subscriber));
|
|
}
|
|
}
|
|
|
|
$endTime = microtime(true);
|
|
|
|
logger('Command Succeeded in ' . ($endTime - $startTime) . ' second(s).');
|
|
|
|
}
|
|
}
|