I have the following listener setup which sends out email notifications to users upon submitting a form. It works perfectly except when I add implements ShouldQueue
it returns an error. I am using Iron.io to fire the events.
If I remove implements ShouldQueue
it works fine. However it takes longer for the page to load. What effect does implements ShouldQueue
have?
<?php
namespace App\Listeners\Timesheet;
use App\Events\Timesheet\TimesheetCreated;
use App\Repos\Email\EmailTemplateRepoInterface;
use App\Repos\User\UserRepoInterface;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail, DbView, Auth;
class EmailTimesheetCreationConfirmation implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct(EmailTemplateRepoInterface $template, UserRepoInterface $user)
{
$this->template = $template;
$this->user = $user;
}
/**
* Handle the event.
*
* @param TimesheetCreated $event
* @return void
*/
public function handle(TimesheetCreated $event)
{
$timesheet = $event->timesheet;
$this->dispatchEmail($timesheet->submitted_by, 1); // send submittor email
$this->dispatchEmail($timesheet->employer_id, 2); // send employer email
$this->dispatchEmail($timesheet->supervisor_id, 3); // send supervisor email
}
/**
*
*
* @param TimesheetCreated $event
* @return void
*/
public function dispatchEmail($id, $templateId)
{
$user = $this->user->getUserNotificationSettingsById($id);
if($user)
{
if($user->notify_create_timesheet)
{
$template = $this->template->findTemplateById(1);
Mail::queue([], [], function ($message) use ($template, $user)
{
$message->to($user->email)
->subject($template->subject)
->setBody(DbView::make($template)->with($user->toArray())->render(), 'text/html');
});
}
}
}
}
The error I get when I use implements ShouldQueue
is below:
exception 'ErrorException' with message 'Undefined property: App\Events\Timesheet\TimesheetCreated::$timesheet' in /home/vagrant/Code/App/public/app/Listeners/Timesheet/EmailTimesheetCreationConfirmation.php:33