SesSdkTransport   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getCredentials() 0 7 2
A doSend() 0 10 4
A __toString() 0 16 3
A doSendSdk() 0 3 1
A getPayload() 0 11 1
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\Ses\Exception\SesException;
16
use Aws\SesV2\SesV2Client;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\Mailer\Envelope;
19
use Symfony\Component\Mailer\Exception\TransportException;
20
use Symfony\Component\Mailer\SentMessage;
21
use Symfony\Component\Mailer\Transport\AbstractTransport;
22
use Symfony\Component\Mime\Email;
23
use Symfony\Component\Mime\MessageConverter;
24
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
25
26
class SesSdkTransport extends AbstractTransport
27
{
28
    private $client;
29
30
    private $credentials;
31
32
    private $config;
33
34
    public function __toString(): string
35
    {
36
        try {
37
            $credentials = $this->getCredentials();
38
        } catch (\Exception $exception) {
39
            $credentials = new Credentials('', '');
40
        }
41
42
        $parameters = http_build_query($this->config->getOptions());
43
44
        return sprintf(
45
            'ses+sdk://%s:%s@%s%s',
46
            urlencode($credentials->getAccessKeyId()),
47
            urlencode($credentials->getSecretKey()),
48
            $this->client->getRegion(),
49
            !empty($parameters) ? '?'.$parameters : ''
50
        );
51
    }
52
53
    public function __construct(
54
        SesSdkTransportConfig $config,
55
        EventDispatcherInterface $dispatcher = null,
56
        LoggerInterface $logger = null,
57
        $handler = null
58
    ) {
59
        $this->config = $config;
60
61
        $this->client = new SesV2Client([
62
            'version' => 'latest',
63
            'region' => $this->config->getRegion(),
64
            'credentials' => $this->config->getCredentials(),
65
            'handler' => $handler,
66
        ]);
67
68
        parent::__construct($dispatcher, $logger);
69
    }
70
71
    protected function doSend(SentMessage $message): void
72
    {
73
        try {
74
            $email = MessageConverter::toEmail($message->getOriginalMessage());
75
            $response = $this->doSendSdk($email, $message->getEnvelope());
76
            $message->setMessageId((string) $response->get('MessageId'));
77
        } catch (SesException $exception) {
78
            $message = $exception->getAwsErrorMessage() ?: $exception->getMessage();
79
            $code = $exception->getStatusCode() ?: $exception->getCode();
80
            throw new TransportException(sprintf('Unable to send an email: %s (code %s).', $message, $code));
81
        }
82
    }
83
84
    protected function doSendSdk(Email $email, Envelope $envelope): \Aws\Result
85
    {
86
        return $this->client->sendEmail($this->getPayload($email, $envelope));
87
    }
88
89
    protected function getPayload(Email $email, Envelope $envelope)
90
    {
91
        return array_merge($this->config->getOptions(), [
92
            'FromEmailAddress' => $envelope->getSender()->toString(),
93
            'Destination' => [
94
                'ToAddresses' => $this->stringifyAddresses($email->getTo()),
95
                'CcAddresses' => $this->stringifyAddresses($email->getCc()),
96
                'BccAddresses' => $this->stringifyAddresses($email->getBcc()),
97
            ],
98
            'Content' => [
99
                'Raw' => ['Data' => $email->toString()],
100
            ],
101
        ]);
102
    }
103
104
    /**
105
     * @return Credentials
106
     */
107
    protected function getCredentials()
108
    {
109
        if (null === $this->credentials) {
110
            $this->credentials = $this->client->getCredentials()->wait();
111
        }
112
113
        return $this->credentials;
114
    }
115
}
116