|
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\SendSmtpEmailSender; |
|
13
|
|
|
use SendinBlue\Client\Model\SendSmtpEmailTo; |
|
14
|
|
|
|
|
15
|
|
|
class SendinblueDriver implements Driver |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** @var SMTPApi|null */ |
|
19
|
|
|
public $client = null; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
public $message = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Driver constructor. |
|
26
|
|
|
* @param array $config |
|
27
|
|
|
* @throws InvalidConfiguration |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $config) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!isset($config['key'])) { |
|
32
|
|
|
throw InvalidConfiguration::invalidCredential('sendinblue', 'key'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$config = Configuration::getDefaultConfiguration()->setApiKey('api-key', $config['key']); |
|
36
|
|
|
$this->client = new SMTPApi(new Client(), $config, null); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return Driver |
|
41
|
|
|
*/ |
|
42
|
|
|
public function make(): Driver |
|
43
|
|
|
{ |
|
44
|
|
|
$this->message = []; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param string $email |
|
52
|
|
|
* @return Driver |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setFrom(string $name, string $email): Driver |
|
55
|
|
|
{ |
|
56
|
|
|
$this->message['sender'] = new SendSmtpEmailSender([ |
|
57
|
|
|
'name' => $name, |
|
58
|
|
|
'email' => $email |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $template |
|
66
|
|
|
* @return Driver |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setTemplate($template): Driver |
|
69
|
|
|
{ |
|
70
|
|
|
$this->message['templateId'] = $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'] = [new SendSmtpEmailTo([ |
|
94
|
|
|
'name' => $name, |
|
95
|
|
|
'email' => $email |
|
96
|
|
|
])]; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $variables |
|
103
|
|
|
* @return Driver |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setVariables(array $variables): Driver |
|
106
|
|
|
{ |
|
107
|
|
|
$params = []; |
|
108
|
|
|
foreach ($variables as $variableKey => $variableValue) { |
|
109
|
|
|
$params[$variableKey] = $variableValue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->message['params'] = $params; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $language |
|
119
|
|
|
* @return Driver |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setLanguage(string $language): Driver |
|
122
|
|
|
{ |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return array |
|
128
|
|
|
* @throws SendError |
|
129
|
|
|
*/ |
|
130
|
|
|
public function send(): array |
|
131
|
|
|
{ |
|
132
|
|
|
$email = new SendSmtpEmail($this->message); |
|
133
|
|
|
|
|
134
|
|
|
try { |
|
135
|
|
|
$response = $this->client->sendTransacEmail($email); |
|
|
|
|
|
|
136
|
|
|
return ['messageId' => $response->getMessageId()]; |
|
137
|
|
|
} catch (ApiException $exception) { |
|
138
|
|
|
throw SendError::responseError('sendinblue', 0, $exception); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function toArray(): array |
|
146
|
|
|
{ |
|
147
|
|
|
return [ |
|
148
|
|
|
'body' => [ |
|
149
|
|
|
'Messages' => [ |
|
150
|
|
|
$this->message, |
|
151
|
|
|
], |
|
152
|
|
|
], |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
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.