1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DansMaCulotte\MailTemplate\Drivers; |
4
|
|
|
|
5
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration; |
6
|
|
|
use DansMaCulotte\MailTemplate\Exceptions\SendError; |
7
|
|
|
use SendGrid; |
8
|
|
|
use SendGrid\Mail\Mail; |
9
|
|
|
use SendGrid\Mail\Substitution; |
10
|
|
|
|
11
|
|
|
class SendgridDriver implements Driver |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var SendGrid|null */ |
15
|
|
|
public $client = null; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
public $body = []; |
19
|
|
|
|
20
|
|
|
/** @var SendGrid\Mail\Mail */ |
21
|
|
|
public $message; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Driver constructor. |
25
|
|
|
* @param array $config |
26
|
|
|
* @throws InvalidConfiguration |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $config) |
29
|
|
|
{ |
30
|
|
|
if (!isset($config['key'])) { |
31
|
|
|
throw InvalidConfiguration::invalidCredential('sendgrid', 'key'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->client = new SendGrid($config['key']); |
35
|
|
|
$this->message = new Mail(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string $email |
41
|
|
|
* @return Driver |
42
|
|
|
* @throws SendGrid\Mail\TypeException |
43
|
|
|
*/ |
44
|
|
|
public function setFrom(string $name, string $email): Driver |
45
|
|
|
{ |
46
|
|
|
$this->message->setFrom($email, $name); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $templateId |
53
|
|
|
* @return Driver |
54
|
|
|
*/ |
55
|
|
|
public function setTemplate(string $templateId): Driver |
56
|
|
|
{ |
57
|
|
|
$this->message->setTemplateId($templateId); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $subject |
64
|
|
|
* @return Driver |
65
|
|
|
*/ |
66
|
|
|
public function setSubject(string $subject): Driver |
67
|
|
|
{ |
68
|
|
|
$this->message->setSubject($subject); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $email |
76
|
|
|
* @return Driver |
77
|
|
|
*/ |
78
|
|
|
public function setRecipient(string $name, string $email): Driver |
79
|
|
|
{ |
80
|
|
|
$this->message->addTo($email, $name); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $variables |
87
|
|
|
* @return Driver |
88
|
|
|
*/ |
89
|
|
|
public function setVariables(array $variables): Driver |
90
|
|
|
{ |
91
|
|
|
foreach ($variables as $key => $value) { |
92
|
|
|
$this->message->addSubstitution($key, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $language |
100
|
|
|
* @return Driver |
101
|
|
|
*/ |
102
|
|
|
public function setLanguage(string $language): Driver |
103
|
|
|
{ |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
* @throws SendError |
110
|
|
|
*/ |
111
|
|
|
public function send(): array |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$response = $this->client->send($this->message); |
|
|
|
|
115
|
|
|
return $response->headers(); |
116
|
|
|
} catch (\Exception $exception) { |
117
|
|
|
throw SendError::responseError('sendgrid', 0, $exception); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function toArray(): array |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
'body' => array_merge($this->body, [ |
128
|
|
|
'message' => $this->message, |
129
|
|
|
]), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
} |
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.