Passed
Push — master ( c371c4...b87560 )
by Byron
02:43
created

SesSdkTransport::doSendSdk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the badams/symfony-amazon-sdk-mailer package.
5
 *
6
 * (c) Byron Adams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badams\AmazonMailerSdk;
13
14
use Aws\Credentials\Credentials;
15
use Aws\Credentials\CredentialsInterface;
16
use Aws\Ses\Exception\SesException;
17
use Aws\SesV2\SesV2Client;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Mailer\Envelope;
20
use Symfony\Component\Mailer\Exception\TransportException;
21
use Symfony\Component\Mailer\SentMessage;
22
use Symfony\Component\Mailer\Transport\AbstractTransport;
23
use Symfony\Component\Mime\Email;
24
use Symfony\Component\Mime\MessageConverter;
25
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
26
27
class SesSdkTransport extends AbstractTransport
28
{
29
    private $client;
30
31
    /**
32
     * @var CredentialsInterface
33
     */
34
    private $credentials;
35
36
    public function __toString(): string
37
    {
38
        try {
39
            $credentials = $this->getCredentials();
40
        } catch (\Exception $exception) {
41
            $credentials = new Credentials('', '');
42
        }
43
44
        return sprintf(
45
            'ses+sdk://%s:%s@%s',
46
            urlencode($credentials->getAccessKeyId()),
47
            urlencode($credentials->getSecretKey()),
48
            $this->client->getRegion()
49
        );
50
    }
51
52
    public function __construct(
53
        callable $credentials,
54
        string $region,
55
        EventDispatcherInterface $dispatcher = null,
56
        LoggerInterface $logger = null,
57
        $handler = null
58
    ) {
59
        $this->client = new SesV2Client([
60
            'version' => 'latest',
61
            'region' => $region,
62
            'credentials' => $credentials,
63
            'handler' => $handler,
64
        ]);
65
66
        parent::__construct($dispatcher, $logger);
67
    }
68
69
    protected function doSend(SentMessage $message): void
70
    {
71
        try {
72
            $email = MessageConverter::toEmail($message->getOriginalMessage());
73
            $response = $this->doSendSdk($email, $message->getEnvelope());
74
            $message->setMessageId($response->get('MessageId'));
0 ignored issues
show
Bug introduced by
It seems like $response->get('MessageId') can also be of type null; however, parameter $id of Symfony\Component\Mailer...Message::setMessageId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            $message->setMessageId(/** @scrutinizer ignore-type */ $response->get('MessageId'));
Loading history...
75
        } catch (SesException $exception) {
76
            throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $exception->getAwsErrorMessage() ?: $exception->getMessage(), $exception->getStatusCode() ?: $exception->getCode()));
77
        }
78
    }
79
80
    protected function doSendSdk(Email $email, Envelope $envelope): \Aws\Result
81
    {
82
        return $this->client->sendEmail($this->getPayload($email, $envelope));
83
    }
84
85
    protected function getPayload(Email $email, Envelope $envelope)
86
    {
87
        return [
88
            'FromEmailAddress' => $envelope->getSender()->toString(),
89
            'Destination' => [
90
                'ToAddresses' => $this->stringifyAddresses($email->getTo()),
91
                'CcAddresses' => $this->stringifyAddresses($email->getCc()),
92
                'BccAddresses' => $this->stringifyAddresses($email->getBcc()),
93
            ],
94
            'Content' => [
95
                'Raw' => ['Data' => $email->toString()],
96
            ],
97
        ];
98
    }
99
100
    public function getCredentials()
101
    {
102
        if (null === $this->credentials) {
103
            $this->credentials = $this->client->getCredentials()->wait();
104
        }
105
106
        return $this->credentials;
107
    }
108
}
109