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