Passed
Branch main (b6a268)
by Iain
04:04
created

MailgunEmailSender::send()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 32
rs 8.9617
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Notification\Sender;
16
17
use Mailgun\Mailgun;
18
use Parthenon\Common\LoggerAwareTrait;
19
use Parthenon\Notification\Configuration;
20
use Parthenon\Notification\EmailInterface;
21
use Parthenon\Notification\EmailSenderInterface;
22
use Parthenon\Notification\Exception\UnableToSendMessageException;
23
24
final class MailgunEmailSender implements EmailSenderInterface
25
{
26
    use LoggerAwareTrait;
27
28
    public function __construct(private Mailgun $mailgun, private string $domain, private Configuration $configuration)
29
    {
30
    }
31
32
    public function send(EmailInterface $message)
33
    {
34
        $messageArray = [
35
            'to' => $message->getToAddress(),
36
            'from' => $message->getFromAddress(),
37
            'subject' => $message->getSubject(),
38
        ];
39
40
        if ($message->isTemplate()) {
41
            $messageArray['template'] = $message->getTemplateName();
42
            $messageArray['h:X-Mailgun-Variables'] = json_encode($message->getTemplateVariables());
43
        } else {
44
            $messageArray['html'] = $message->getContent();
45
        }
46
47
        $attachments = $message->getAttachments();
48
        if (!empty($attachments)) {
49
            $messageArray['attachment'] = [];
50
            foreach ($attachments as $attachment) {
51
                $messageArray['attachment'][] = ['fileContent' => $attachment->getContent(), 'filename' => $attachment->getName()];
52
            }
53
        }
54
55
        try {
56
            $response = $this->mailgun->messages()->send($this->domain, $messageArray);
57
        } catch (\Exception $e) {
58
            throw new UnableToSendMessageException($e->getMessage(), $e->getCode(), $e);
59
        }
60
61
        if (200 !== $response->getStatusCode()) {
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Mailgun\Model\Message\SendResponse. ( Ignorable by Annotation )

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

61
        if (200 !== $response->/** @scrutinizer ignore-call */ getStatusCode()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            $this->getLogger()->warning('Unable to send email via mailgun', ['status_code' => $response->getStatusCode()]);
63
            throw new UnableToSendMessageException(sprintf('Mailgun returned %d status code', $response->getStatusCode()));
64
        }
65
    }
66
}
67