1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
class PushwooshMessage implements JsonSerializable |
12
|
|
|
{ |
13
|
|
|
protected $androidRootParameters; |
14
|
|
|
protected $campaign; |
15
|
|
|
protected $content; |
16
|
|
|
protected $data; |
17
|
|
|
protected $identifier; |
18
|
|
|
protected $iosRootParameters; |
19
|
|
|
protected $preset; |
20
|
|
|
protected $recipientTimezone; |
21
|
|
|
protected $shortenUrl; |
22
|
|
|
protected $timezone; |
23
|
|
|
protected $throughput; |
24
|
|
|
protected $url; |
25
|
|
|
protected $when; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new push message. |
29
|
|
|
* |
30
|
|
|
* @param string $content |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
90 |
|
public function __construct(string $content = '') |
34
|
|
|
{ |
35
|
90 |
|
$this->content = $content; |
36
|
90 |
|
$this->recipientTimezone = false; |
37
|
90 |
|
$this->when = 'now'; |
38
|
90 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Associate the message to the given notification. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
12 |
|
public function associate(Notification $notification) |
47
|
|
|
{ |
48
|
12 |
|
if (!$this->identifier) { |
49
|
12 |
|
$this->identifier = $notification->id; |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the Pushwoosh campaign code. |
57
|
|
|
* |
58
|
|
|
* @param string $campaign |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
6 |
|
public function campaign(string $campaign) |
62
|
|
|
{ |
63
|
6 |
|
$this->campaign = $campaign; |
64
|
|
|
|
65
|
6 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the message content. |
70
|
|
|
* |
71
|
|
|
* @param string $content |
72
|
|
|
* @param string|null $language |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
6 |
|
public function content(string $content, string $language = null) |
76
|
|
|
{ |
77
|
6 |
|
if ($language) { |
78
|
6 |
|
if (!is_array($this->content)) { |
|
|
|
|
79
|
6 |
|
$this->content = []; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
$this->content[$language] = $content; |
83
|
|
|
} else { |
84
|
6 |
|
$this->content = $content; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the delivery moment. |
92
|
|
|
* |
93
|
|
|
* @param \DateTimeInterface|string $when |
94
|
|
|
* @param \DateTimeZone|string|null $timezone |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
6 |
|
public function deliverAt($when, $timezone = null) |
98
|
|
|
{ |
99
|
6 |
|
if ($when instanceof DateTimeInterface) { |
100
|
6 |
|
$timezone = $when->getTimezone(); |
101
|
6 |
|
$when = $when->format('Y-m-d H:i'); |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
if ($timezone instanceof DateTimeZone) { |
105
|
6 |
|
$timezone = $timezone->getName(); |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
$this->timezone = $timezone; |
109
|
6 |
|
$this->when = $when; |
110
|
|
|
|
111
|
6 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the message identifier. |
116
|
|
|
* |
117
|
|
|
* @param string $identifier |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
12 |
|
public function identifier(string $identifier) |
121
|
|
|
{ |
122
|
12 |
|
$this->identifier = $identifier; |
123
|
|
|
|
124
|
12 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Convert the message into something JSON serializable. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
78 |
|
public function jsonSerialize() |
133
|
|
|
{ |
134
|
|
|
$payload = [ |
135
|
78 |
|
'android_root_params' => $this->androidRootParameters, |
136
|
78 |
|
'campaign' => $this->campaign, |
137
|
78 |
|
'content' => $this->content, |
138
|
78 |
|
'data' => $this->data, |
139
|
78 |
|
'ignore_user_timezone' => !$this->recipientTimezone, |
140
|
78 |
|
'ios_root_params' => $this->iosRootParameters, |
141
|
78 |
|
'link' => $this->url, |
142
|
78 |
|
'minimize_link' => $this->url ? $this->shortenUrl : null, |
143
|
78 |
|
'preset' => $this->preset, |
144
|
78 |
|
'send_date' => $this->when, |
145
|
78 |
|
'send_rate' => $this->throughput, |
146
|
78 |
|
'transactionId' => $this->identifier, |
147
|
78 |
|
'timezone' => $this->timezone, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
return array_filter($payload, function ($value) { |
151
|
78 |
|
return $value !== null; |
152
|
78 |
|
}); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the Pushwoosh preset code. |
157
|
|
|
* |
158
|
|
|
* @param string $preset |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
6 |
|
public function preset(string $preset) |
162
|
|
|
{ |
163
|
6 |
|
$this->preset = $preset; |
164
|
|
|
|
165
|
6 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Throttle the message rollout. |
170
|
|
|
* |
171
|
|
|
* @param int $limit |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
6 |
|
public function throttle(int $limit) |
175
|
|
|
{ |
176
|
6 |
|
$this->throughput = max(100, min($limit, 1000)); |
177
|
|
|
|
178
|
6 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set the URL the message should link to. |
183
|
|
|
* |
184
|
|
|
* @param string $url |
185
|
|
|
* @param bool $shorten |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
6 |
|
public function url(string $url, bool $shorten = true) |
189
|
|
|
{ |
190
|
6 |
|
$this->shortenUrl = $shorten; |
191
|
6 |
|
$this->url = $url; |
192
|
|
|
|
193
|
6 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Add a root level parameter. |
198
|
|
|
* |
199
|
|
|
* @param string $key |
200
|
|
|
* @param mixed $value |
201
|
|
|
* @param string|null $platform |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
18 |
|
public function with(string $key, $value, string $platform = null) |
205
|
|
|
{ |
206
|
18 |
|
if (!in_array($platform, [null, 'ios', 'android'])) { |
207
|
|
|
throw new InvalidArgumentException("Invalid platform {$platform}"); |
208
|
|
|
} |
209
|
|
|
|
210
|
18 |
|
if (($platform ?: 'android') === 'android') { |
211
|
12 |
|
$this->androidRootParameters[$key] = $value; |
212
|
12 |
|
$this->data[$key] = $value; # android_root_params seems to (not always) work |
213
|
|
|
} |
214
|
|
|
|
215
|
18 |
|
if (($platform ?: 'ios') === 'ios') { |
216
|
12 |
|
$this->iosRootParameters[$key] = $value; |
217
|
|
|
} |
218
|
|
|
|
219
|
18 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Respect the recipients' timezone when delivering. |
224
|
|
|
* |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
6 |
|
public function useRecipientTimezone() |
228
|
|
|
{ |
229
|
6 |
|
$this->recipientTimezone = true; |
230
|
|
|
|
231
|
6 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|