Completed
Push — master ( 46962a...2b767b )
by Craig
02:11
created

PostmarkTransport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Coconuts\Mail;
4
5
use Swift_Attachment;
6
use Swift_Mime_SimpleMessage;
7
use GuzzleHttp\ClientInterface;
8
use Illuminate\Mail\Transport\Transport;
9
10
class PostmarkTransport extends Transport
11
{
12
    /**
13
     * Guzzle client instance.
14
     *
15
     * @var \GuzzleHttp\ClientInterface
16
     */
17
    protected $client;
18
19
    /**
20
     * The Postmark API key.
21
     *
22
     * @var string
23
     */
24
    protected $key;
25
26
    /**
27
     * The Postmark API end-point.
28
     *
29
     * @var string
30
     */
31
    protected $url = 'https://api.postmarkapp.com/email';
32
33
    /**
34
     * Create a new Postmark transport instance.
35
     *
36
     * @param \GuzzleHttp\ClientInterface $client
37
     * @param string $key
38
     *
39
     * @return void
40
     */
41 4
    public function __construct(ClientInterface $client, $key)
42
    {
43 4
        $this->key = $key;
44 4
        $this->client = $client;
45 4
    }
46
47
    /**
48
     * Send the given Message.
49
     *
50
     * Recipient/sender data will be retrieved from the Message API.
51
     * The return value is the number of recipients who were accepted for delivery.
52
     *
53
     * @param Swift_Mime_SimpleMessage $message
54
     * @param string[] $failedRecipients An array of failures by-reference
55
     *
56
     * @return int
57
     */
58 1
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
59
    {
60 1
        $this->beforeSendPerformed($message);
61
62 1
        $response = $this->client->post($this->url, $this->payload($message));
63
64 1
        $message->getHeaders()->addTextHeader(
65 1
            'X-PM-Message-Id',
66 1
            $this->getMessageId($response)
67
        );
68
69 1
        $this->sendPerformed($message);
70
71 1
        return $this->numberOfRecipients($message);
72
    }
73
74
    /**
75
     * Get all attachments for the given message.
76
     *
77
     * @param \Swift_Mime_SimpleMessage $message
78
     *
79
     * @return array
80
     */
81 2
    protected function getAttachments(Swift_Mime_SimpleMessage $message)
82
    {
83 2
        $attachments = [];
84
85 2
        $children = $message->getChildren();
86
87 2
        foreach ($children as $child) {
88 2
            if ($child instanceof Swift_Attachment) {
89 2
                $header = $child->getHeaders()->get('content-type');
90
91 2
                $attachments[] = [
92 2
                    'Name' => $header->getParameter('name'),
93 2
                    'Content' => base64_encode($child->getBody()),
94 2
                    'ContentType' => $child->getContentType(),
95
                ];
96
            }
97
        }
98
99 2
        return $attachments;
100
    }
101
102
    /**
103
     * Format the contacts for the API request
104
     *
105
     * @param array $contacts
106
     *
107
     * @return string
108
     */
109 3
    protected function getContacts($contacts)
110
    {
111 3
        return collect($contacts)
112 3
            ->map(function ($display, $address) {
113 3
                return $display ? $display." <{$address}>" : $address;
114 3
            })
115 3
            ->values()
116 3
            ->implode(',');
117
    }
118
119
    /**
120
     * Get the message ID from the response.
121
     *
122
     * @param \GuzzleHttp\Psr7\Response $response
123
     *
124
     * @return string
125
     */
126 1
    protected function getMessageId($response)
127
    {
128 1
        return object_get(
129 1
            json_decode($response->getBody()->getContents()),
130 1
            'MessageID'
131
        );
132
    }
133
134
    /**
135
     * Get the HTTP payload for sending the Postmark message.
136
     *
137
     * @param \Swift_Mime_SimpleMessage $message
138
     *
139
     * @return array
140
     */
141 2
    protected function payload(Swift_Mime_SimpleMessage $message)
142
    {
143 2
        $headers = $message->getHeaders();
144
145 2
        $to = $this->getContacts($message->getTo());
146 2
        $from = $this->getContacts($message->getFrom());
147 2
        $cc = $this->getContacts($message->getCc());
148 2
        $bcc = $this->getContacts($message->getBcc());
149 2
        $replyTo = $this->getContacts($message->getReplyTo());
1 ignored issue
show
Documentation introduced by
$message->getReplyTo() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150 2
        $attachments = $this->getAttachments($message);
151
152
        return [
153
            'headers' => [
154 2
                'Accept' => 'application/json',
155 2
                'X-Postmark-Server-Token' => $this->key,
156
            ],
157
            'json' => [
158 2
                'From' => $from,
159 2
                'To' => $to,
160 2
                'Cc' => $cc,
161 2
                'Bcc' => $bcc,
162 2
                'Tag' => $headers->has('tag') ? $headers->get('tag')->getFieldBody() : '',
163 2
                'Subject' => $message->getSubject(),
164 2
                'HtmlBody' => $message->getBody(),
165 2
                'ReplyTo' => $replyTo,
166 2
                'Attachments' => $attachments,
167
            ],
168
        ];
169
    }
170
}
171