Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SendEmailJob | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| getJobType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Job; |
| 5 | |
| 6 | use Cake\Mailer\Mailer; |
| 7 | use Cake\Queue\Job\Message; |
| 8 | use Exception; |
| 9 | use Interop\Queue\Processor; |
| 10 | |
| 11 | /** |
| 12 | * SendEmailJob Class |
| 13 | * |
| 14 | * This class is responsible for processing and sending emails as a background job. |
| 15 | * It fetches email templates from the database, replaces placeholders with provided data, |
| 16 | * and sends the email using CakePHP's Mailer class. |
| 17 | */ |
| 18 | class SendEmailJob extends AbstractJob |
| 19 | { |
| 20 | /** |
| 21 | * Get the human-readable job type name for logging |
| 22 | * |
| 23 | * @return string The job type description |
| 24 | */ |
| 25 | protected static function getJobType(): string |
| 26 | { |
| 27 | return 'email sending'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Executes the email sending process using the provided message data. |
| 32 | * |
| 33 | * @param \Cake\Queue\Job\Message $message The message object containing email details |
| 34 | * @return string|null Returns Processor::ACK if successful, Processor::REJECT if failed |
| 35 | */ |
| 36 | public function execute(Message $message): ?string |
| 37 | { |
| 38 | $templateIdentifier = $message->getArgument('template_identifier'); |
| 39 | $from = $message->getArgument('from'); |
| 40 | $to = $message->getArgument('to'); |
| 41 | $viewVars = $message->getArgument('viewVars', []); |
| 42 | |
| 43 | if (!$this->validateArguments($message, ['template_identifier', 'from', 'to'])) { |
| 44 | return Processor::REJECT; |
| 45 | } |
| 46 | |
| 47 | return $this->executeWithErrorHandling( |
| 48 | $templateIdentifier, |
| 49 | function () use ($templateIdentifier, $from, $to, $viewVars) { |
| 50 | // Fetch the email template from the database |
| 51 | $emailTemplatesTable = $this->getTable('EmailTemplates'); |
| 52 | $emailTemplate = $emailTemplatesTable->find() |
| 53 | ->where(['template_identifier' => $templateIdentifier]) |
| 54 | ->first(); |
| 55 | |
| 56 | if (!$emailTemplate) { |
| 57 | throw new Exception(__('Email template not found: {0}', $templateIdentifier)); |
| 58 | } |
| 59 | |
| 60 | // Replace placeholders in the email body |
| 61 | foreach ($viewVars as $key => $value) { |
| 62 | $emailTemplate->body_html = str_replace('{' . $key . '}', $value, $emailTemplate->body_html); |
| 63 | $emailTemplate->body_plain = str_replace('{' . $key . '}', $value, $emailTemplate->body_plain); |
| 64 | } |
| 65 | |
| 66 | // Configure and send email |
| 67 | $mailer = new Mailer('default'); |
| 68 | $mailer->setTo($to) |
| 69 | ->setFrom($from) |
| 70 | ->setSubject($emailTemplate->subject) |
| 71 | ->setEmailFormat('both') |
| 72 | ->setViewVars([ |
| 73 | 'bodyHtml' => $emailTemplate->body_html, |
| 74 | 'bodyPlain' => $emailTemplate->body_plain, |
| 75 | ]) |
| 76 | ->viewBuilder() |
| 77 | ->setTemplate('default') |
| 78 | ->setLayout('default') |
| 79 | ->setPlugin('AdminTheme'); |
| 80 | |
| 81 | return $mailer->deliver(); |
| 82 | }, |
| 83 | "{$from} → {$to}", |
| 84 | ); |
| 85 | } |
| 86 | } |