|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DansMaCulotte\MailTemplate\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration; |
|
6
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\SendError; |
|
7
|
|
|
use Mandrill; |
|
8
|
|
|
use Mandrill_Error; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class MandrillDriver |
|
12
|
|
|
* @package DansMaCulotte\MailTemplate\Drivers |
|
13
|
|
|
*/ |
|
14
|
|
|
class MandrillDriver implements Driver |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Mandrill|null */ |
|
17
|
|
|
public $client = null; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
public $body = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
public $message = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* MandrillDriver constructor. |
|
27
|
|
|
* @param $config |
|
28
|
|
|
* @throws InvalidConfiguration |
|
29
|
|
|
* @throws Mandrill_Error |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($config) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!isset($config['secret'])) { |
|
34
|
|
|
throw InvalidConfiguration::invalidCredential('mandrill', 'secret'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->client = new Mandrill($config['secret']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return Driver |
|
42
|
|
|
*/ |
|
43
|
|
|
public function make(): Driver |
|
44
|
|
|
{ |
|
45
|
|
|
$this->body = []; |
|
46
|
|
|
$this->message = []; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param string $email |
|
54
|
|
|
* @return Driver |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setFrom(string $name, string $email): Driver |
|
57
|
|
|
{ |
|
58
|
|
|
$this->message['from_name'] = $name; |
|
59
|
|
|
$this->message['from_email'] = $email; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $template |
|
66
|
|
|
* @return Driver |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setTemplate($template): Driver |
|
69
|
|
|
{ |
|
70
|
|
|
$this->body['template'] = $template; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $subject |
|
77
|
|
|
* @return Driver |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setSubject(string $subject): Driver |
|
80
|
|
|
{ |
|
81
|
|
|
$this->message['subject'] = $subject; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* @param string $email |
|
89
|
|
|
* @return Driver |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setRecipient(string $name, string $email): Driver |
|
92
|
|
|
{ |
|
93
|
|
|
$this->message['to'][] = [ |
|
94
|
|
|
'name' => $name, |
|
95
|
|
|
'email' => $email, |
|
96
|
|
|
'type' => 'to', |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $variables |
|
104
|
|
|
* @return Driver |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setVariables(array $variables): Driver |
|
107
|
|
|
{ |
|
108
|
|
|
foreach ($variables as $variableKey => $variableValue) { |
|
109
|
|
|
$this->message['global_merge_vars'][] = [ |
|
110
|
|
|
'name' => strtoupper($variableKey), |
|
111
|
|
|
'content' => $variableValue, |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $language |
|
120
|
|
|
* @return Driver |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setLanguage(string $language): Driver |
|
123
|
|
|
{ |
|
124
|
|
|
$this->message['global_merge_vars'][] = [ |
|
125
|
|
|
'name' => 'MC_LANGUAGE', |
|
126
|
|
|
'content' => $language, |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return array |
|
135
|
|
|
* @throws SendError |
|
136
|
|
|
*/ |
|
137
|
|
|
public function send(): array |
|
138
|
|
|
{ |
|
139
|
|
|
$response = []; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
try { |
|
142
|
|
|
$response = $this->client->messages->sendTemplate( |
|
143
|
|
|
$this->body['template'], |
|
144
|
|
|
[], |
|
145
|
|
|
$this->message |
|
146
|
|
|
); |
|
147
|
|
|
} catch (Mandrill_Error $exception) { |
|
148
|
|
|
throw SendError::responseError('mandrill', 0, $exception); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $response; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function toArray(): array |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
'body' => [ |
|
158
|
|
|
'message' => $this->message, |
|
159
|
|
|
], |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|