1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class PushwooshPendingMessage implements \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
protected $client; |
10
|
|
|
protected $messages; |
11
|
|
|
protected $recipients; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new pending message. |
15
|
|
|
* |
16
|
|
|
* @param \NotificationChannels\Pushwoosh\Pushwoosh $client |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
30 |
|
public function __construct(Pushwoosh $client) |
20
|
|
|
{ |
21
|
30 |
|
$this->client = $client; |
22
|
30 |
|
$this->messages = new Collection(); |
23
|
30 |
|
$this->recipients = new Collection(); |
24
|
30 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Send the message. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
30 |
|
public function __destruct() |
32
|
|
|
{ |
33
|
30 |
|
if ($this->messages->isEmpty()) { |
34
|
24 |
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
$this->client->createMessage($this); |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Convert the queued messages to something JSON serializable. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
18 |
|
public function jsonSerialize() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
18 |
|
'application' => $this->client->getApplicationCode(), |
49
|
18 |
|
'auth' => $this->client->getApiToken(), |
50
|
|
|
'notifications' => $this->messages->map(function (PushwooshMessage $message) { |
51
|
|
|
return $this->stamp($message); |
52
|
18 |
|
}), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Queue the message. |
58
|
|
|
* |
59
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshMessage $message |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
6 |
|
public function queue(PushwooshMessage $message) |
63
|
|
|
{ |
64
|
6 |
|
$this->messages->push($message); |
65
|
|
|
|
66
|
6 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Stamp the message before sending it. |
71
|
|
|
* |
72
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshMessage $message |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function stamp(PushwooshMessage $message) |
76
|
|
|
{ |
77
|
|
|
$recipients = $this->recipients->reduce(function (array $stamps, PushwooshRecipient $recipient) { |
78
|
|
|
return array_merge($stamps, $recipient->jsonSerialize()); |
79
|
|
|
}, []); |
80
|
|
|
|
81
|
|
|
return array_merge($message->jsonSerialize(), $recipients); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the recipient(s) for the queued messages. |
86
|
|
|
* |
87
|
|
|
* @param \NotificationChannels\Pushwoosh\PushwooshRecipient $recipient |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function to(PushwooshRecipient $recipient) |
91
|
|
|
{ |
92
|
|
|
$this->recipients->push($recipient); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tell the message it was sent. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
6 |
|
public function wasSent() |
103
|
|
|
{ |
104
|
6 |
|
$this->messages = new Collection(); |
105
|
6 |
|
} |
106
|
|
|
} |
107
|
|
|
|