1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Entity/EmailEntity.php

196 lines
5.3 KiB
PHP
Raw Normal View History

2020-10-27 01:05:42 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2020-10-27 12:57:12 +01:00
namespace App\Jobs\Entity;
2020-10-27 01:05:42 +01:00
use App\DataMapper\Analytics\EmailInvoiceFailure;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Helpers\Email\InvoiceEmail;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\CreditInvitation;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Models\SystemLog;
2020-10-27 12:57:12 +01:00
use App\Utils\HtmlEngine;
2020-10-27 01:05:42 +01:00
use App\Utils\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
2020-10-27 12:57:12 +01:00
use Illuminate\Support\Str;
2020-10-28 11:10:49 +01:00
use Swift_TransportException;
2020-10-27 01:05:42 +01:00
use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains;
use Turbo124\Beacon\Facades\LightLogs;
/*Multi Mailer implemented*/
class EmailEntity extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invitation;
public $company;
public $settings;
public $entity_string;
2020-10-27 12:57:12 +01:00
public $reminder_template;
public $entity;
public $html_engine;
public $email_entity_builder;
2020-11-05 11:14:30 +01:00
public $template_data;
2020-10-27 01:05:42 +01:00
/**
* EmailEntity constructor.
* @param Invitation $invitation
2020-10-27 12:57:12 +01:00
* @param Company $company
* @param ?string $reminder_template
2020-10-27 01:05:42 +01:00
*/
2020-11-05 11:14:30 +01:00
public function __construct($invitation, Company $company, ?string $reminder_template = null, ?array $template_data = null)
2020-10-27 01:05:42 +01:00
{
$this->company = $company;
$this->invitation = $invitation;
$this->settings = $invitation->contact->client->getMergedSettings();
$this->entity_string = $this->resolveEntityString();
2020-10-27 12:57:12 +01:00
$this->entity = $invitation->{$this->entity_string};
2020-11-04 10:32:49 +01:00
$this->reminder_template = $reminder_template ?: $this->entity->calculateTemplate($this->entity_string);
2020-10-27 12:57:12 +01:00
$this->html_engine = new HtmlEngine($invitation);
2020-11-05 11:14:30 +01:00
$this->template_data = $template_data;
2020-10-27 12:57:12 +01:00
$this->email_entity_builder = $this->resolveEmailBuilder();
2020-11-05 11:14:30 +01:00
2020-10-27 01:05:42 +01:00
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
2020-11-01 04:19:03 +01:00
if($this->company->is_disabled)
return true;
2020-10-27 01:05:42 +01:00
MultiDB::setDB($this->company->db);
$this->setMailDriver();
try {
2020-10-28 11:10:49 +01:00
/** @noinspection PhpMethodParametersCountMismatchInspection */
2020-10-27 01:05:42 +01:00
Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name())
->send(
new TemplateEmail(
2020-10-27 12:57:12 +01:00
$this->email_entity_builder,
2020-10-27 01:05:42 +01:00
$this->invitation->contact->user,
$this->invitation->contact->client
)
);
2020-10-28 11:10:49 +01:00
} catch (Swift_TransportException $e) {
2020-10-27 01:05:42 +01:00
$this->entityEmailFailed($e->getMessage());
}
if (count(Mail::failures()) > 0) {
2020-10-27 12:57:12 +01:00
$this->logMailError(Mail::failures(), $this->entity->client);
2020-10-27 01:05:42 +01:00
} else {
$this->entityEmailSucceeded();
}
2020-10-27 12:57:12 +01:00
/* Mark entity sent */
$this->entity->service()->markSent()->save();
2020-10-27 01:05:42 +01:00
}
public function failed($exception = null)
{
info('the job failed');
$job_failure = new EmailInvoiceFailure();
2020-10-27 12:57:12 +01:00
$job_failure->string_metric5 = $this->entity_string;
2020-10-27 01:05:42 +01:00
$job_failure->string_metric6 = $exception->getMessage();
LightLogs::create($job_failure)
->batch();
}
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';
}
private function entityEmailFailed($message)
{
switch ($this->entity_string) {
case 'invoice':
event(new InvoiceWasEmailedAndFailed($this->invitation->invoice, $this->company, $message, Ninja::eventVars()));
break;
2020-10-28 11:10:49 +01:00
2020-10-27 01:05:42 +01:00
default:
# code...
break;
}
}
private function entityEmailSucceeded()
{
switch ($this->entity_string) {
case 'invoice':
event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars()));
break;
2020-10-28 11:10:49 +01:00
2020-10-27 01:05:42 +01:00
default:
# code...
break;
}
}
2020-10-27 12:57:12 +01:00
private function findReminderTemplate()
{
}
private function resolveEmailBuilder()
{
$class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine";
2020-11-05 11:14:30 +01:00
return (new $class($this->invitation, $this->reminder_template, $this->template_data))->build();
2020-10-27 12:57:12 +01:00
}
2020-10-27 01:05:42 +01:00
}