1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AramisAuto\EmailController\PayloadDecoder; |
4
|
|
|
|
5
|
|
|
use AramisAuto\EmailController\Exception\InvalidPayloadException; |
6
|
|
|
use AramisAuto\EmailController\Exception\MandrillWebhookTestException; |
7
|
|
|
use AramisAuto\EmailController\Message; |
8
|
|
|
|
9
|
|
|
class MandrillPayloadDecoder implements PayloadDecoderInterface |
10
|
|
|
{ |
11
|
|
|
public function decode($payload) |
12
|
|
|
{ |
13
|
|
|
// Parse request raw body |
14
|
|
|
$vars = array(); |
15
|
|
|
parse_str($payload, $vars); |
16
|
|
|
|
17
|
|
|
if (!isset($vars['mandrill_events'])) { |
18
|
|
|
throw new InvalidPayloadException( |
19
|
|
|
sprintf( |
20
|
|
|
'Missing "mandrill_events" body parameter - %s', |
21
|
|
|
json_encode(array('payload' => $payload), JSON_UNESCAPED_SLASHES) |
22
|
|
|
) |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Decode JSON |
27
|
|
|
$messagesMandrill = json_decode($vars['mandrill_events'], true); |
28
|
|
|
|
29
|
|
|
if (is_array($messagesMandrill) && count($messagesMandrill) === 0) { |
30
|
|
|
throw new MandrillWebhookTestException( |
31
|
|
|
sprintf( |
32
|
|
|
'Received Mandrill test payload - %s', |
33
|
|
|
json_encode(array('payload' => $payload), JSON_UNESCAPED_SLASHES) |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!$messagesMandrill) { |
39
|
|
|
throw new InvalidPayloadException( |
40
|
|
|
sprintf( |
41
|
|
|
'Could not decode payload JSON - %s', |
42
|
|
|
json_encode($vars['mandrill_events'], JSON_UNESCAPED_SLASHES) |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$messages = array(); |
48
|
|
|
foreach ($messagesMandrill as $messageMandrill) { |
|
|
|
|
49
|
|
|
// Defaults |
50
|
|
|
$messageDefaults = array( |
51
|
|
|
'cc' => array(), |
52
|
|
|
'from_email' => null, |
53
|
|
|
'from_name' => null, |
54
|
|
|
'headers' => array(), |
55
|
|
|
'html' => null, |
56
|
|
|
'id' => null, |
57
|
|
|
'metadata' => array(), |
58
|
|
|
'raw_msg' => null, |
59
|
|
|
'subject' => null, |
60
|
|
|
'text' => null, |
61
|
|
|
'email' => array(), |
62
|
|
|
); |
63
|
|
|
$messageFields = array_merge($messageDefaults, $messageMandrill); |
64
|
|
|
|
65
|
|
|
// Create EmailController message |
66
|
|
|
$message = new Message(); |
67
|
|
|
$message->setRaw($messageFields['raw_msg']); |
68
|
|
|
$message->setHeaders($messageFields['headers']); |
|
|
|
|
69
|
|
|
$message->setText($messageFields['text']); |
70
|
|
|
$message->setHtml($messageFields['html']); |
71
|
|
|
$message->setSubject($messageFields['subject']); |
72
|
|
|
$message->setFrom($messageFields['msg']['sender']); |
73
|
|
|
$message->setTo(array(array($messageMandrill['msg']['email'], null))); |
74
|
|
|
|
75
|
|
|
// Message unique identifier |
76
|
|
|
$message->setId($messageFields['_id']); |
77
|
|
|
|
78
|
|
|
// Message metadata |
79
|
|
|
$message->metadata = array(); |
80
|
|
|
if (isset($messageFields['msg'])) { |
81
|
|
|
$message->metadata = array_merge($message->metadata, $messageFields['msg']); |
82
|
|
|
} |
83
|
|
|
if (isset($messageFields['msg']) && isset($messageFields['msg']['metadata'])) { |
84
|
|
|
$message->metadata = array_merge($message->metadata, $messageFields['msg']['metadata']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Keep original message |
88
|
|
|
$message->source = $messageMandrill; |
89
|
|
|
|
90
|
|
|
$messages[] = $message; |
91
|
|
|
unset($messageFields); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $messages; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.