|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* YAWIK |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://github.com/cross-solution/YAWIK for the canonical source repository |
|
7
|
|
|
* @copyright https://github.com/cross-solution/YAWIK/blob/master/COPYRIGHT |
|
8
|
|
|
* @license https://github.com/cross-solution/YAWIK/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Core\Queue\Strategy; |
|
14
|
|
|
|
|
15
|
|
|
use Core\Mail\MailService; |
|
16
|
|
|
use Core\Queue\Job\ExceptionJobResult; |
|
17
|
|
|
use Core\Queue\Job\JobResult; |
|
18
|
|
|
use Core\Queue\Job\MailSenderInterface; |
|
19
|
|
|
use Core\Queue\Job\ResultProviderInterface; |
|
20
|
|
|
use Laminas\EventManager\EventManagerInterface; |
|
21
|
|
|
use SlmQueue\Strategy\AbstractStrategy; |
|
22
|
|
|
use SlmQueue\Worker\Event\AbstractWorkerEvent; |
|
23
|
|
|
use SlmQueue\Worker\Event\ProcessJobEvent; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* TODO: description |
|
27
|
|
|
* |
|
28
|
|
|
* @author Mathias Gelhausen |
|
29
|
|
|
* TODO: write tests |
|
30
|
|
|
*/ |
|
31
|
|
|
class SendMailStrategy extends AbstractStrategy |
|
32
|
|
|
{ |
|
33
|
|
|
private $mailService; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(MailService $mailService) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->mailService = $mailService; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Registers itself with an EventManager |
|
42
|
|
|
* |
|
43
|
|
|
* @param EventManagerInterface $events |
|
44
|
|
|
* @param int $priority |
|
45
|
|
|
*/ |
|
46
|
|
|
public function attach(EventManagerInterface $events, $priority = 1) : void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->listeners[] = $events->attach(AbstractWorkerEvent::EVENT_PROCESS_JOB, [$this, 'sendMail'], -900); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function sendMail(ProcessJobEvent $event) |
|
52
|
|
|
{ |
|
53
|
|
|
$job = $event->getJob(); |
|
54
|
|
|
|
|
55
|
|
|
if (!$job instanceof MailSenderInterface) { |
|
56
|
|
|
$event->setResult(ProcessJobEvent::JOB_STATUS_FAILURE); |
|
57
|
|
|
if ($job instanceof ResultProviderInterface) { |
|
58
|
|
|
$job->setResult(JobResult::failure('This queue can only consume Jobs which implement the ' . MailSenderInterface::class)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->mailService->send($job->getMail()); |
|
64
|
|
|
} catch (\Throwable $e) { |
|
65
|
|
|
|
|
66
|
|
|
$event->setResult(ProcessJobEvent::JOB_STATUS_FAILURE); |
|
67
|
|
|
if ($job instanceof ResultProviderInterface) { |
|
68
|
|
|
$job->setResult(new ExceptionJobResult($e)); |
|
69
|
|
|
} |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$event->setResult(ProcessJobEvent::JOB_STATUS_SUCCESS); |
|
74
|
|
|
if ($job instanceof ResultProviderInterface) { |
|
75
|
|
|
$job->setResult(JobResult::success('Mail send successfully.')); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|