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\queue; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use craft\helpers\ArrayHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 2.3.2 |
18
|
|
|
*/ |
19
|
|
|
class EmailByKey extends AbstractEmailByKey implements \Serializable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The email key |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The recipients |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $recipients = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The email params |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $params; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function getKey(): string |
46
|
|
|
{ |
47
|
|
|
return $this->key; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function getRecipients(): array |
54
|
|
|
{ |
55
|
|
|
return $this->recipients; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $recipients |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setRecipients(array $recipients) |
63
|
|
|
{ |
64
|
|
|
foreach ($recipients as $recipient) { |
65
|
|
|
$this->addRecipient($recipient); |
66
|
|
|
} |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $recipient |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function addRecipient($recipient) |
75
|
|
|
{ |
76
|
|
|
if ($recipient instanceof User) { |
77
|
|
|
$recipient = [$recipient->email]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->recipients[] = $recipient; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function getParams(): array |
88
|
|
|
{ |
89
|
|
|
return $this->params; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $params |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setParams($params) |
97
|
|
|
{ |
98
|
|
|
$this->params = ArrayHelper::toArray($params); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param User|int|string|array $user |
104
|
|
|
* @return User |
105
|
|
|
*/ |
106
|
|
|
protected function resolveUser($user) |
107
|
|
|
{ |
108
|
|
|
if ($user instanceof User) { |
109
|
|
|
return $user; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// An Id |
113
|
|
|
if (is_numeric($user)) { |
114
|
|
|
return Craft::$app->getUsers()->getUserById($user); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// An email |
118
|
|
|
if (is_string($user)) { |
119
|
|
|
if (!$element = Craft::$app->getUsers()->getUserByUsernameOrEmail($user)) { |
120
|
|
|
$element = new User([ |
121
|
|
|
'email' => $user |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $element; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (is_array($user)) { |
129
|
|
|
$email = key($user); |
130
|
|
|
$user = reset($user); |
131
|
|
|
|
132
|
|
|
// $user was an array [email => name] |
133
|
|
|
if (is_string($email)) { |
134
|
|
|
@list($firstName, $lastName) = explode(' ', $user); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// Resolve user (and set name) |
137
|
|
|
if ($element = $this->resolveUser($email)) { |
138
|
|
|
$element->firstName = $firstName ?: $element->firstName; |
139
|
|
|
$element->lastName = $lastName ?: $element->lastName; |
140
|
|
|
return $element; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return new User([ |
144
|
|
|
'email' => $email, |
145
|
|
|
'firstName' => $firstName, |
146
|
|
|
'lastName' => $lastName |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// An array of [$user] |
151
|
|
|
if (!$element = $this->resolveUser($user)) { |
152
|
|
|
return new User([ |
153
|
|
|
'email' => $user |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $element; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
|
|
public function serialize() |
167
|
|
|
{ |
168
|
|
|
return serialize([ |
169
|
|
|
'key' => $this->key, |
170
|
|
|
'recipients' => $this->recipients, |
171
|
|
|
'params' => $this->params |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritdoc |
177
|
|
|
*/ |
178
|
|
|
public function unserialize($serialized) |
179
|
|
|
{ |
180
|
|
|
Craft::configure( |
181
|
|
|
$this, |
182
|
|
|
unserialize($serialized) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: