Passed
Pull Request — master (#7)
by
unknown
03:17
created

AmazonSesWrapper::prepareMailer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ByJG\Mail\Wrapper;
4
5
use ByJG\Mail\Override\PHPMailerOverride;
6
use Aws\Credentials\Credentials;
7
use Aws\Ses\SesClient;
8
use ByJG\Mail\Envelope;
9
10
class AmazonSesWrapper extends PHPMailerWrapper
11
{
12
13
    /**
14
     * @param Envelope $envelope
15
     * @return PHPMailerOverride
16
     * @throws \phpmailerException
17
     */
18 4
    protected function prepareMailer(Envelope $envelope)
19
    {
20 4
        $mail = parent::prepareMailer($envelope);
21 4
        $mail->Subject = $envelope->getSubject();
22 4
        return $mail;
23
    }
24
25
    /**
26
     * @return SesClient
27
     */
28 1
    public function getSesClient()
29
    {
30
        //Send the message (which must be base 64 encoded):
31 1
        return new SesClient([
32 1
            'credentials' => new Credentials(
33 1
                $this->uri->getUsername(),
34 1
                $this->uri->getPassword()
35
            ),
36 1
            'region' => $this->uri->getHost(),
37 1
            'version' => '2010-12-01'
38
        ]);
39
    }
40
41
    /**
42
     * ses://accessid:aswsecret@region
43
     *
44
     * @param Envelope $envelope
45
     * @return bool
46
     * @throws \Exception
47
     */
48 4
    public function send(Envelope $envelope)
49
    {
50 4
        $this->validate($envelope);
51
52 4
        $mail = $this->prepareMailer($envelope);
53
54
        // Call the preSend to set all PHPMailer variables and get the correct header and body;
55 4
        $message = $mail->getFullMessageEnvelope();
56
57 4
        $ses = $this->getSesClient();
58
59 4
        $ses->sendRawEmail(
60
            [
61
                'RawMessage' => [
62 4
                    'Data' => $message,
63
                ]
64
            ]
65
        );
66
67 4
        return true;
68
    }
69
}
70