1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DansMaCulotte\MailTemplate\Drivers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration; |
7
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\SendError; |
8
|
|
|
use Mailgun\Exception\HttpClientException; |
9
|
|
|
use Mailgun\Mailgun; |
10
|
|
|
|
11
|
|
|
class MailgunDriver implements Driver |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var Mailgun|null */ |
15
|
|
|
public $client = null; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
public $body = []; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
public $message = []; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
public $domain; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Driver constructor. |
28
|
|
|
* @param array $config |
29
|
|
|
* @throws InvalidConfiguration |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $config) |
32
|
|
|
{ |
33
|
|
|
if (!isset($config['key'])) { |
34
|
|
|
throw InvalidConfiguration::invalidCredential('mailgun', 'key'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!isset($config['domain'])) { |
38
|
|
|
throw InvalidConfiguration::invalidCredential('mailgun', 'domain'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->client = Mailgun::create($config['key']); |
42
|
|
|
$this->domain = $config['domain']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
* @param string $email |
48
|
|
|
* @return Driver |
49
|
|
|
*/ |
50
|
|
|
public function setFrom(string $name, string $email): Driver |
51
|
|
|
{ |
52
|
|
|
$this->message['from'] = "${name} <${email}>"; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $template |
59
|
|
|
* @return Driver |
60
|
|
|
*/ |
61
|
|
|
public function setTemplate(string $template): Driver |
62
|
|
|
{ |
63
|
|
|
$this->message['template'] = $template; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $subject |
70
|
|
|
* @return Driver |
71
|
|
|
*/ |
72
|
|
|
public function setSubject(string $subject): Driver |
73
|
|
|
{ |
74
|
|
|
$this->message['subject'] = $subject; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $name |
81
|
|
|
* @param string $email |
82
|
|
|
* @return Driver |
83
|
|
|
*/ |
84
|
|
|
public function setRecipient(string $name, string $email): Driver |
85
|
|
|
{ |
86
|
|
|
$this->message['to'] = "${name} <${email}>"; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $variables |
93
|
|
|
* @return Driver |
94
|
|
|
*/ |
95
|
|
|
public function setVariables(array $variables): Driver |
96
|
|
|
{ |
97
|
|
|
$this->message['h:X-Mailgun-Variables'] = json_encode($variables); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $language |
104
|
|
|
* @return Driver |
105
|
|
|
*/ |
106
|
|
|
public function setLanguage(string $language): Driver |
107
|
|
|
{ |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
* @throws SendError |
114
|
|
|
*/ |
115
|
|
|
public function send(): array |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$response = $this->client->messages()->send( |
|
|
|
|
119
|
|
|
$this->domain, |
120
|
|
|
$this->message |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return [ |
124
|
|
|
'id' => $response->getId(), |
125
|
|
|
'message' => $response->getMessage(), |
126
|
|
|
]; |
127
|
|
|
} catch (HttpClientException $exception) { |
128
|
|
|
throw SendError::responseError('mailgun', 0, $exception); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function toArray(): array |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
'body' => array_merge($this->body, [ |
139
|
|
|
'Messages' => [ |
140
|
|
|
$this->message, |
141
|
|
|
], |
142
|
|
|
]) |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.