]> _ Git - psq.git/commitdiff
ProcessEmailBatch.php
authorLouis Jeckel <louis.jeckel@outlook.com>
Wed, 13 Jan 2021 18:45:12 +0000 (19:45 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Wed, 13 Jan 2021 18:45:12 +0000 (19:45 +0100)
app/Jobs/ProcessEmailBatch.php

index 3585676ca997d1d986863a8f734e5d90f80c174b..5ce434efa29a001be5463fafce38d0248e9b05b8 100644 (file)
@@ -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;
     }
 }