dimanche 25 mars 2018

Laravel job for Mailables, common object

I create Laravel Job for sending emails and I have multiple Mailable classes. Can I create object whose point to my Mailable classes?

My Job class:

class SendMail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $emailObject;

    public function __construct($emailObject)
    {
        $this->emailObject = $emailObject;
    }

    public function handle()
    {
        \Mail::to($this->emailObject->user->email)->queue($this->emailObject);
    }
}

I execute this with:

 $this->dispatch(new SendMail(new Profile($user)));

or

$this->dispatch(new SendMail(new Newsletter($user)));

I would like to create object or pattern which will allow me to create constructor with object in my job. Like this:

class SendMail{

        public function __construct(CommonMailObject $emailObject)
        {
            $this->emailObject = $emailObject;
        }
}

It's a good way?

eg. Profile class:

class Profile extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }


    public function build()
    {
        return $this->from('', '')
                    ->subject('')
                    ->view(EmailType::PROFILE);
    }
}

Aucun commentaire:

Enregistrer un commentaire