Lae/app/Jobs/User/SendUserNotificationsJob.php

61 lines
1.5 KiB
PHP
Raw Normal View History

2022-12-31 10:04:28 +00:00
<?php
2023-01-13 14:11:56 +00:00
namespace App\Jobs\User;
2022-12-31 10:04:28 +00:00
use App\Http\Controllers\Admin\NotificationController;
use App\Models\User;
2023-01-13 14:11:56 +00:00
use App\Notifications\User\UserNotification;
2022-12-31 10:04:28 +00:00
use GeneaLabs\LaravelModelCaching\CachedBuilder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-01-13 14:11:56 +00:00
class SendUserNotificationsJob implements ShouldQueue
2022-12-31 10:04:28 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected array $requests;
2023-01-30 16:14:07 +00:00
2022-12-31 10:04:28 +00:00
protected User|CachedBuilder $users;
2023-01-30 16:14:07 +00:00
protected string $title;
protected string $content;
2023-01-13 14:11:56 +00:00
protected bool $send_mail;
2022-12-31 10:04:28 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2023-01-13 14:11:56 +00:00
public function __construct(array $requests, $title, $content, $send_mail = false)
2022-12-31 10:04:28 +00:00
{
$this->requests = $requests;
$this->title = $title;
$this->content = $content;
2023-01-13 14:11:56 +00:00
$this->send_mail = $send_mail;
2022-12-31 10:04:28 +00:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle(): void
{
$notificationController = new NotificationController();
$users = $notificationController->query($this->requests);
// chunk
$users->chunk(100, function ($users) {
foreach ($users as $user) {
2023-01-13 14:11:56 +00:00
$user->notify(new UserNotification($this->title, $this->content, $this->send_mail));
2022-12-31 10:04:28 +00:00
}
});
}
}