From 838fd31af5383ca1897b16eef7224380dbb6c484 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Wed, 13 Jan 2021 19:45:12 +0100 Subject: [PATCH] ProcessEmailBatch.php --- app/Jobs/ProcessEmailBatch.php | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/app/Jobs/ProcessEmailBatch.php b/app/Jobs/ProcessEmailBatch.php index 3585676..5ce434e 100644 --- a/app/Jobs/ProcessEmailBatch.php +++ b/app/Jobs/ProcessEmailBatch.php @@ -67,6 +67,7 @@ class ProcessEmailBatch implements ShouldQueue private bool $attachFile = false; + private array $templateVariables; /** @@ -81,7 +82,7 @@ class ProcessEmailBatch implements ShouldQueue $this->users = $usersBuilder->get(); - $this->replaceVariables(); + } /** @@ -128,19 +129,6 @@ class ProcessEmailBatch implements ShouldQueue ]))); } - private function replaceVariables() - { - if($this->html === null) { - return; - } - - $replace = [ - '{{name}}' => '%recipient.name%', - ]; - - $this->html = str_replace(array_keys($replace), array_values($replace), $this->html); - } - /** * Execute the job. * @@ -168,17 +156,19 @@ class ProcessEmailBatch implements ShouldQueue $this->batch->file->publish(); } - $templateVariables = $this->processVariablesForMailgun(); + $this->html = $this->batch->render(); + + $this->templateVariables = $this->processVariablesForMailgun(); foreach($chunks as $chunk) { - $variables = json_encode($chunk->mapWithKeys(function (User $user) use ($templateVariables){ + $variables = json_encode($chunk->mapWithKeys(function (User $user) { $fileVar = $this->batch->file === null ? [] : ['file_url' => $this->batch->file->getMailableUrl($user)]; - $filledVariables = array_map(fn($template) => $user->templateAttribute($template), $templateVariables); + $filledVariables = array_map(fn($template) => $user->templateAttribute($template), $this->templateVariables); return [ @@ -191,15 +181,13 @@ class ProcessEmailBatch implements ShouldQueue - $view = $this->batch->render(); - $params = [ 'from' => config('mail.mailgun.from_name').' <'.config('mail.from.address').'>', 'to' => $chunk->pluck('email')->toArray(), 'subject' => $this->batch->subject, 'recipient-variables' => $variables, - 'html' => $view, + 'html' => $this->html, 'o:tag' => [$this->batch->getTag(), config('app.env').'_batch_'], 'o:testmode' => config('mail.mailgun.test_mode', 'yes'), 'h:Reply-To' => 'olivier.robichon@prescription-quotidien.com', @@ -235,10 +223,10 @@ class ProcessEmailBatch implements ShouldQueue $this->html = preg_replace_callback('/\{\{[^(% )]*\}\}/', function($matches) use(&$variables){ $match = Str::before(Str::after($matches[0], '{{'), '}}'); - $variables[] = $match ; + $variables[$match] = $match ; return "%recipient.$match%"; }, $this->html); - return array_unique($variables); + return $variables; } } -- 2.39.5