Completed
Push — master ( c0d5d3...41182a )
by Craig
03:15
created

PostmarkTransport::getContacts()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Coconuts\Mail;
4
5
use Swift_Mime_Message;
6
use GuzzleHttp\ClientInterface;
7
use Illuminate\Mail\Transport\Transport;
8
9
class PostmarkTransport extends Transport
10
{
11
    /**
12
     * Guzzle client instance.
13
     *
14
     * @var \GuzzleHttp\ClientInterface
15
     */
16
    protected $client;
17
18
    /**
19
     * The Postmark API key.
20
     *
21
     * @var string
22
     */
23
    protected $key;
24
25
    /**
26
     * The Postmark API end-point.
27
     *
28
     * @var string
29
     */
30
    protected $url = 'https://api.postmarkapp.com/email';
31
32
    /**
33
     * Create a new Postmark transport instance.
34
     *
35
     * @param \GuzzleHttp\ClientInterface $client
36
     * @param string $key
37
     *
38
     * @return void
39
     */
40 5
    public function __construct(ClientInterface $client, $key)
41
    {
42 5
        $this->key = $key;
43 5
        $this->client = $client;
44 5
    }
45
46
    /**
47
     * Send the given Message.
48
     *
49
     * Recipient/sender data will be retrieved from the Message API.
50
     * The return value is the number of recipients who were accepted for delivery.
51
     *
52
     * @param Swift_Mime_Message $message
53
     * @param string[] $failedRecipients An array of failures by-reference
54
     *
55
     * @return int
56
     */
57 1
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
58
    {
59 1
        $this->beforeSendPerformed($message);
60
61 1
        $this->client->post($this->url, $this->payload($message));
62
63 1
        $this->sendPerformed($message);
64
65 1
        return $this->numberOfRecipients($message);
66
    }
67
68
    /**
69
     * Format the contacts for the API request
70
     *
71
     * @param array $contacts
72
     *
73
     * @return string
74
     */
75 3
    protected function getContacts($contacts)
76
    {
77 3
        return collect($contacts)
78
            ->map(function ($display, $address) {
79 3
                return $display ? $display . " <{$address}>" : $address;
80 3
            })
81 3
            ->values()
82 3
            ->implode(',');
83
    }
84
85
    /**
86
     * Get the "From" payload field for the API request.
87
     *
88
     * @param \Swift_Mime_Message $message
89
     *
90
     * @return string
91
     */
92 3
    protected function getFrom(Swift_Mime_Message $message)
93
    {
94 3
        return collect($message->getFrom())
95 3
            ->map(function ($display, $address) {
96 3
                return $display ? $display . " <$address>" : $address;
97 3
            })
98 3
            ->values()
99 3
            ->implode(',');
100
    }
101
102
    /**
103
     * Get the HTTP payload for sending the Postmark message.
104
     *
105
     * @param \Swift_Mime_Message $message
106
     *
107
     * @return array
108
     */
109 2
    protected function payload(Swift_Mime_Message $message)
110
    {
111 2
        $headers = $message->getHeaders();
112
113 2
        $to = $this->getContacts($message->getTo());
114 2
        $cc = $this->getContacts($message->getCc());
115 2
        $bcc = $this->getContacts($message->getBcc());
116
117
        return [
118
            'headers' => [
119 2
                'Accept' => 'application/json',
120 2
                'X-Postmark-Server-Token' => $this->key,
121
            ],
122
            'json' => [
123 2
                'From' => $this->getFrom($message),
124 2
                'To' => $to,
125 2
                'Cc' => $cc,
126 2
                'Bcc' => $bcc,
127 2
                'Tag' => $headers->has('tag') ? $headers->get('tag')->getFieldBody() : '',
128 2
                'Subject' => $message->getSubject(),
129 2
                'HtmlBody' => $message->getBody(),
130
            ],
131
        ];
132
    }
133
}
134