forked from dereuromark/cakephp-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailerTask.php
More file actions
68 lines (56 loc) · 1.76 KB
/
Copy pathMailerTask.php
File metadata and controls
68 lines (56 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Queue\Queue\Task;
use Cake\Log\Log;
use Cake\Mailer\MailerAwareTrait;
use Queue\Model\QueueException;
use Queue\Queue\Task;
use Throwable;
/**
* A convenience task ready to use for asynchronously sending reusable emails via Mailer classes.
*
* Especially useful is the fact that sending is auto-retried as per your config.
* Will not drop the email if successfully sent, you can decide to even retry manually again afterwards.
*
* @author Mark Scherer
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class MailerTask extends Task {
use MailerAwareTrait;
/**
* @var int
*/
public $timeout = 60;
/**
* @var \Cake\Mailer\Mailer
*/
protected $mailer;
/**
* @param array<string, mixed> $data The array passed to QueuedJobsTable::createJob()
* @param int $jobId The id of the QueuedJob entity
* @throws \Queue\Model\QueueException
* @throws \Cake\Mailer\Exception\MissingMailerException
* @throws \Throwable
* @return void
*/
public function run(array $data, int $jobId): void {
if (!isset($data['class'])) {
throw new QueueException('Queue Mailer task called without valid `mailer` class.');
}
if (!isset($data['action'])) {
throw new QueueException('Queue Mailer task called without `action` data.');
}
$this->mailer = $this->getMailer($data['class']);
try {
$this->mailer->setTransport($data['transport'] ?? 'default');
$result = $this->mailer->send($data['action'], $data['vars'] ?? []);
} catch (Throwable $e) {
$error = $e->getMessage();
$error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL . $e->getTraceAsString();
Log::write('error', $error);
throw $e;
}
if (!$result) {
throw new QueueException('Could not send email.');
}
}
}