1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\queries; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use craft\helpers\ArrayHelper; |
14
|
|
|
use craft\helpers\StringHelper; |
15
|
|
|
use craft\queue\BaseJob; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 2.3.2 |
21
|
|
|
*/ |
22
|
|
|
class EmailByKey extends BaseJob implements \Serializable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The email key |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The recipients |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $recipients = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The email params |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $params; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
protected function defaultDescription() |
50
|
|
|
{ |
51
|
|
|
return 'Sending an email by key.'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
|
public function execute($queue) |
59
|
|
|
{ |
60
|
|
|
// A random tracking string |
61
|
|
|
$id = StringHelper::randomString(); |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
Craft::info( |
65
|
|
|
sprintf( |
66
|
|
|
"[Sending email via job '%s'.] %s", |
67
|
|
|
(string)$id, |
68
|
|
|
(string)Json::encode( |
69
|
|
|
[ |
70
|
|
|
'tracking' => $id, |
71
|
|
|
'key' => $this->key, |
72
|
|
|
'recipients' => $this->recipients |
73
|
|
|
] |
74
|
|
|
) |
75
|
|
|
), __METHOD__ |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
foreach ($this->recipients as $recipient) { |
79
|
|
|
if (null === ($recipient = $this->resolveUser($recipient))) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @var User $recipient */ |
84
|
|
|
|
85
|
|
|
if (!Craft::$app->getMailer() |
86
|
|
|
->composeFromKey( |
87
|
|
|
$this->key, |
88
|
|
|
array_merge( |
89
|
|
|
['recipient' => $recipient], |
90
|
|
|
$this->params |
91
|
|
|
) |
92
|
|
|
)->setTo($recipient) |
93
|
|
|
->send() |
94
|
|
|
) { |
95
|
|
|
Craft::error( |
96
|
|
|
sprintf( |
97
|
|
|
"Failed to send email via job '%s'", |
98
|
|
|
(string)$id |
99
|
|
|
), __METHOD__ |
100
|
|
|
); |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
Craft::info( |
105
|
|
|
sprintf( |
106
|
|
|
"Successfully sent email via job '%s'", |
107
|
|
|
(string)$id |
108
|
|
|
), __METHOD__ |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} catch (\Exception $e) { |
113
|
|
|
Craft::error( |
114
|
|
|
sprintf( |
115
|
|
|
"Exception caught while trying to run '%s' (Id: %s) job. Exception: [%s].", |
116
|
|
|
(string)get_class($this), |
117
|
|
|
$id, |
118
|
|
|
(string)Json::encode([ |
119
|
|
|
'Trace' => $e->getTraceAsString(), |
120
|
|
|
'File' => $e->getFile(), |
121
|
|
|
'Line' => $e->getLine(), |
122
|
|
|
'Code' => $e->getCode(), |
123
|
|
|
'Message' => $e->getMessage() |
124
|
|
|
]) |
125
|
|
|
), __METHOD__ |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
throw $e; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $recipients |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setRecipients(array $recipients) |
137
|
|
|
{ |
138
|
|
|
foreach ($recipients as $recipient) { |
139
|
|
|
$this->addRecipient($recipient); |
140
|
|
|
} |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $recipient |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function addRecipient($recipient) |
149
|
|
|
{ |
150
|
|
|
if ($recipient instanceof User) { |
151
|
|
|
$recipient = [$recipient->email]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->recipients[] = $recipient; |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $params |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setParams($params) |
163
|
|
|
{ |
164
|
|
|
$this->params = ArrayHelper::toArray($params); |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param User|int|string|array $user |
170
|
|
|
* @return User |
171
|
|
|
*/ |
172
|
|
|
function resolveUser($user) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
if ($user instanceof User) { |
175
|
|
|
return $user; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// An Id |
179
|
|
|
if (is_numeric($user)) { |
180
|
|
|
return Craft::$app->getUsers()->getUserById($user); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// An email |
184
|
|
|
if (is_string($user)) { |
185
|
|
|
if (!$element = Craft::$app->getUsers()->getUserByUsernameOrEmail($user)) { |
186
|
|
|
$element = new User([ |
187
|
|
|
'email' => $user |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $element; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (is_array($user)) { |
195
|
|
|
$email = key($user); |
196
|
|
|
$user = reset($user); |
197
|
|
|
|
198
|
|
|
// $user was an array [email => name] |
199
|
|
|
if (is_string($email)) { |
200
|
|
|
@list($firstName, $lastName) = explode(' ', $user); |
|
|
|
|
201
|
|
|
|
202
|
|
|
// Resolve user (and set name) |
203
|
|
|
if ($element = $this->resolveUser($email)) { |
204
|
|
|
$element->firstName = $firstName ?: $element->firstName; |
205
|
|
|
$element->lastName = $lastName ?: $element->lastName; |
206
|
|
|
return $element; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return new User([ |
210
|
|
|
'email' => $email, |
211
|
|
|
'firstName' => $firstName, |
212
|
|
|
'lastName' => $lastName |
213
|
|
|
]); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// An array of [$user] |
217
|
|
|
if (!$element = $this->resolveUser($user)) { |
218
|
|
|
return new User([ |
219
|
|
|
'email' => $user |
220
|
|
|
]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $element; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @inheritdoc |
231
|
|
|
*/ |
232
|
|
|
public function serialize() |
233
|
|
|
{ |
234
|
|
|
return serialize([ |
235
|
|
|
'key' => $this->key, |
236
|
|
|
'recipients' => $this->recipients, |
237
|
|
|
'params' => $this->params |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @inheritdoc |
243
|
|
|
*/ |
244
|
|
|
public function unserialize($serialized) |
245
|
|
|
{ |
246
|
|
|
Craft::configure( |
247
|
|
|
$this, |
248
|
|
|
unserialize($serialized) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.