Passed
Push — master ( 696cbf...968eef )
by Melech
05:53 queued 01:56
created

MailgunMailer::addAttachments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Mail;
15
16
use Mailgun\Mailgun;
17
use Mailgun\Message\BatchMessage;
18
use Mailgun\Message\Exceptions\MissingRequiredParameter;
19
use Valkyrja\Mail\Contract\Mailer as Contract;
20
use Valkyrja\Mail\Data\Contract\Message;
21
22
/**
23
 * Class MailgunMailer.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
class MailgunMailer implements Contract
28
{
29
    /**
30
     * MailgunMailer constructor.
31
     *
32
     * @param non-empty-string $domain
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
33
     */
34
    public function __construct(
35
        protected Mailgun $mailgun,
36
        protected string $domain,
37
    ) {
38
    }
39
40
    /**
41
     * @inheritDoc
42
     *
43
     * @throws MissingRequiredParameter
44
     */
45
    public function send(Message $message): void
46
    {
47
        $domain = $this->domain;
48
49
        $mailgunMessage = $this->mailgun->messages()->getBatchMessage($domain);
50
        $replyTo        = $message->getReplyToRecipients()[0] ?? null;
51
        $from           = [['email' => $message->getFromEmail(), 'name' => $message->getFromName()]];
52
53
        $mailgunMessage->setSubject($message->getSubject());
54
        $mailgunMessage->setTextBody($message->getBody());
55
56
        if ($message->isHtml()) {
57
            $mailgunMessage->setHtmlBody($message->getBody());
58
            $mailgunMessage->setTextBody($message->getPlainBody() ?? '');
59
        }
60
61
        $this->setRecipients($mailgunMessage, 'setFromAddress', $from);
62
        $this->setRecipients($mailgunMessage, 'addCcRecipient', $message->getCopyRecipients());
63
        $this->setRecipients($mailgunMessage, 'addBccRecipient', $message->getBlindCopyRecipients());
64
        $this->addAttachments($mailgunMessage, $message->getAttachments());
65
66
        if ($replyTo !== null) {
67
            $this->setRecipients($mailgunMessage, 'setReplyToAddress', [$replyTo]);
68
        }
69
70
        $this->setRecipients($mailgunMessage, 'addToRecipient', $message->getRecipients());
71
72
        $mailgunMessage->finalize();
73
    }
74
75
    /**
76
     * Add recipients to a mailgun batch method by method.
77
     *
78
     * @param BatchMessage                                   $mailgunMessage The mailgun batch message
79
     * @param string                                         $method         The method to call
80
     * @param array<int, array{email: string, name: string}> $recipients     The recipients
81
     *
82
     * @return void
83
     */
84
    protected function setRecipients(BatchMessage $mailgunMessage, string $method, array $recipients): void
85
    {
86
        foreach ($recipients as $recipient) {
87
            $mailgunMessage->{$method}($recipient['email'], ['full_name' => $recipient['name']]);
88
        }
89
    }
90
91
    /**
92
     * Add attachments to a mailgun batch message.
93
     *
94
     * @param BatchMessage                                  $mailgunMessage The mailgun batch message
95
     * @param array<int, array{path: string, name: string}> $attachments    The attachments
96
     *
97
     * @return void
98
     */
99
    protected function addAttachments(BatchMessage $mailgunMessage, array $attachments): void
100
    {
101
        foreach ($attachments as $attachment) {
102
            $mailgunMessage->addAttachment($attachment['path'], $attachment['name']);
103
        }
104
    }
105
}
106