company = $company; $this->invitation = $invitation; $this->entity_string = $this->resolveEntityString(); $this->entity = $invitation->{$this->entity_string}; $this->settings = $invitation->contact->client->getMergedSettings(); $this->reminder_template = $reminder_template ?: $this->entity->calculateTemplate($this->entity_string); $this->html_engine = new HtmlEngine($invitation); $this->template_data = $template_data; } /** * Execute the job. * * * @return void */ public function handle(): void { /* Don't fire emails if the company is disabled */ if ($this->company->is_disabled) { return; } $this->email_entity_builder = $this->resolveEmailBuilder(); /* Set DB */ MultiDB::setDB($this->company->db); App::forgetInstance('translator'); $t = app('translator'); App::setLocale($this->invitation->contact->preferredLocale()); $t->replace(Ninja::transformTranslations($this->settings)); /* Mark entity sent */ $this->entity->service()->markSent()->save(); $nmo = new NinjaMailerObject(); $nmo->mailable = new TemplateEmail($this->email_entity_builder, $this->invitation->contact->withoutRelations(), $this->invitation->withoutRelations()); $nmo->company = $this->company->withoutRelations(); $nmo->settings = $this->settings; $nmo->to_user = $this->invitation->contact->withoutRelations(); $nmo->entity_string = $this->entity_string; $nmo->invitation = $this->invitation->withoutRelations(); $nmo->reminder_template = $this->reminder_template; $nmo->entity = $this->entity->withoutRelations(); NinjaMailerJob::dispatch($nmo); $nmo = null; $this->invitation = null; $this->company = null; $this->entity_string = null; $this->entity = null; $this->settings = null; $this->reminder_template = null; $this->html_engine = null; $this->template_data = null; $this->email_entity_builder = null; } private function resolveEntityString(): string { if ($this->invitation instanceof InvoiceInvitation) { return 'invoice'; } elseif ($this->invitation instanceof QuoteInvitation) { return 'quote'; } elseif ($this->invitation instanceof CreditInvitation) { return 'credit'; } elseif ($this->invitation instanceof RecurringInvoiceInvitation) { return 'recurring_invoice'; } return ''; } /* Builds the email builder object */ private function resolveEmailBuilder() { $class = 'App\Mail\Engine\\'.ucfirst(Str::camel($this->entity_string)).'EmailEngine'; return (new $class($this->invitation, $this->reminder_template, $this->template_data))->build(); } public function failed($e) { nlog("EmailEntity"); nlog($e->getMessage()); } }