Passed
Push — master ( ceaf1d...c77583 )
by Mathias
14:53
created

SendMailStrategy::sendMail()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 19
c 1
b 1
f 0
dl 0
loc 30
rs 8.8333
cc 7
nc 36
nop 1
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);
0 ignored issues
show
Bug Best Practice introduced by
The property listeners does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
            $mail = $job->getMail();
64
            if (is_array($mail)) {
65
                $this->mailService->send(...$mail);
0 ignored issues
show
Bug introduced by
$mail is expanded, but the parameter $mail of Core\Mail\MailService::send() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                $this->mailService->send(/** @scrutinizer ignore-type */ ...$mail);
Loading history...
66
            } else {
67
                $this->mailService->send($mail);
68
            }
69
        } catch (\Throwable $e) {
70
71
            $event->setResult(ProcessJobEvent::JOB_STATUS_FAILURE);
72
            if ($job instanceof ResultProviderInterface) {
73
                $job->setResult(new ExceptionJobResult($e));
74
            }
75
            return;
76
        }
77
78
        $event->setResult(ProcessJobEvent::JOB_STATUS_SUCCESS);
79
        if ($job instanceof ResultProviderInterface) {
80
            $job->setResult(JobResult::success('Mail send successfully.'));
81
        }
82
    }
83
}
84