Fixes for php cs fixer email.php

This commit is contained in:
David Bomba 2023-03-13 18:04:24 +11:00
parent 7e81f53a04
commit ea319858e7

View File

@ -11,36 +11,37 @@
namespace App\Services\Email; namespace App\Services\Email;
use App\DataMapper\Analytics\EmailFailure; use App\Models\User;
use App\DataMapper\Analytics\EmailSuccess; use App\Utils\Ninja;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\Vendor;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\User;
use App\Models\Vendor;
use App\Models\VendorContact;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\Ninja; use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\VendorContact;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\VendorHtmlEngine; use App\Utils\VendorHtmlEngine;
use App\Libraries\Google\Google;
use Illuminate\Support\Facades\Mail;
use App\Services\Email\EmailMailable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Queue\SerializesModels;
use Turbo124\Beacon\Facades\LightLogs;
use Illuminate\Queue\InteractsWithQueue;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable; use App\DataMapper\Analytics\EmailFailure;
use App\DataMapper\Analytics\EmailSuccess;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Mail\Mailable; use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use Illuminate\Queue\InteractsWithQueue; use App\Events\Payment\PaymentWasEmailedAndFailed;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;
use Turbo124\Beacon\Facades\LightLogs;
class Email implements ShouldQueue class Email implements ShouldQueue
{ {
@ -78,6 +79,7 @@ class Email implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
$this->setOverride() $this->setOverride()
@ -85,13 +87,13 @@ class Email implements ShouldQueue
->setDefaults() ->setDefaults()
->buildMailable(); ->buildMailable();
if ($this->preFlightChecksFail()) { if($this->preFlightChecksFail())
return; return;
}
$this->email(); $this->email();
$this->tearDown(); $this->tearDown();
} }
/** /**
@ -113,6 +115,7 @@ class Email implements ShouldQueue
*/ */
public function initModels(): self public function initModels(): self
{ {
$this->email_object->entity_id ? $this->email_object->entity = $this->email_object->entity_class::withTrashed()->with('invitations')->find($this->email_object->entity_id) : $this->email_object->entity = null; $this->email_object->entity_id ? $this->email_object->entity = $this->email_object->entity_class::withTrashed()->with('invitations')->find($this->email_object->entity_id) : $this->email_object->entity = null;
$this->email_object->invitation_id ? $this->email_object->invitation = $this->email_object->entity->invitations()->where('id', $this->email_object->invitation_id)->first() : $this->email_object->invitation = null; $this->email_object->invitation_id ? $this->email_object->invitation = $this->email_object->entity->invitations()->where('id', $this->email_object->invitation_id)->first() : $this->email_object->invitation = null;
@ -123,10 +126,13 @@ class Email implements ShouldQueue
$this->email_object->vendor_id ? $this->email_object->vendor = Vendor::withTrashed()->find($this->email_object->vendor_id) : $this->email_object->vendor = null; $this->email_object->vendor_id ? $this->email_object->vendor = Vendor::withTrashed()->find($this->email_object->vendor_id) : $this->email_object->vendor = null;
if (!$this->email_object->contact) { if (!$this->email_object->contact)
{
$this->email_object->vendor_contact_id ? $this->email_object->contact = VendorContact::withTrashed()->find($this->email_object->vendor_contact_id) : null; $this->email_object->vendor_contact_id ? $this->email_object->contact = VendorContact::withTrashed()->find($this->email_object->vendor_contact_id) : null;
$this->email_object->client_contact_id ? $this->email_object->contact = ClientContact::withTrashed()->find($this->email_object->client_contact_id) : null; $this->email_object->client_contact_id ? $this->email_object->contact = ClientContact::withTrashed()->find($this->email_object->client_contact_id) : null;
} }
$this->email_object->user_id ? $this->email_object->user = User::withTrashed()->find($this->email_object->user_id) : $this->email_object->user = $this->company->owner(); $this->email_object->user_id ? $this->email_object->user = User::withTrashed()->find($this->email_object->user_id) : $this->email_object->user = $this->company->owner();
@ -157,7 +163,7 @@ class Email implements ShouldQueue
{ {
$_variables = $this->email_object->variables; $_variables = $this->email_object->variables;
match (class_basename($this->email_object->entity)) { match(class_basename($this->email_object->entity)){
"Invoice" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(), "Invoice" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(),
"Quote" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(), "Quote" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(),
"Credit" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(), "Credit" => $this->email_object->variables = (new HtmlEngine($this->email_object->invitation))->makeValues(),
@ -166,7 +172,8 @@ class Email implements ShouldQueue
}; };
/** If we have passed some variable overrides we insert them here */ /** If we have passed some variable overrides we insert them here */
foreach ($_variables as $key => $value) { foreach($_variables as $key => $value)
{
$this->email_object->variables[$key] = $value; $this->email_object->variables[$key] = $value;
} }
@ -180,6 +187,7 @@ class Email implements ShouldQueue
*/ */
private function tearDown(): self private function tearDown(): self
{ {
$this->email_object->entity = null; $this->email_object->entity = null;
$this->email_object->invitation = null; $this->email_object->invitation = null;
$this->email_object->client = null; $this->email_object->client = null;
@ -189,6 +197,7 @@ class Email implements ShouldQueue
$this->email_object->settings = null; $this->email_object->settings = null;
return $this; return $this;
} }
/** /**
@ -198,9 +207,11 @@ class Email implements ShouldQueue
*/ */
public function setDefaults(): self public function setDefaults(): self
{ {
(new EmailDefaults($this))->run(); (new EmailDefaults($this))->run();
return $this; return $this;
} }
/** /**
@ -210,9 +221,11 @@ class Email implements ShouldQueue
*/ */
public function buildMailable(): self public function buildMailable(): self
{ {
$this->mailable = new EmailMailable($this->email_object); $this->mailable = new EmailMailable($this->email_object);
return $this; return $this;
} }
/** /**
@ -222,6 +235,7 @@ class Email implements ShouldQueue
*/ */
public function email() public function email()
{ {
$this->setMailDriver(); $this->setMailDriver();
/* Init the mailer*/ /* Init the mailer*/
@ -246,9 +260,22 @@ class Email implements ShouldQueue
LightLogs::create(new EmailSuccess($this->company->company_key)) LightLogs::create(new EmailSuccess($this->company->company_key))
->send(); ->send();
} catch (\Exception | \RuntimeException | \Google\Service\Exception $e) {
nlog("Mailer failed with {$e->getMessage()}");
} catch(\Symfony\Component\Mime\Exception\RfcComplianceException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail();
$this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first());
return;
} catch(\Symfony\Component\Mime\Exception\LogicException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail();
$this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first());
return;
} catch (\Exception | \RuntimeException | \Google\Service\Exception $e) {
nlog("Mailer failed with {$e->getMessage()}");
$message = $e->getMessage(); $message = $e->getMessage();
if (stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) { if (stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413')) {
@ -261,7 +288,6 @@ class Email implements ShouldQueue
return; return;
} }
/** /**
* Post mark buries the proper message in a a guzzle response * Post mark buries the proper message in a a guzzle response
* this merges a text string with a json object * this merges a text string with a json object
@ -300,6 +326,7 @@ class Email implements ShouldQueue
} }
$this->cleanUpMailers(); $this->cleanUpMailers();
} }
/** /**
@ -738,10 +765,13 @@ class Email implements ShouldQueue
public function failed($exception = null) public function failed($exception = null)
{ {
if ($exception) {
if($exception)
nlog($exception->getMessage()); nlog($exception->getMessage());
}
config(['queue.failed.driver' => null]); config(['queue.failed.driver' => null]);
} }
} }