1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify\Notification; |
11
|
|
|
|
12
|
|
|
use Jgut\Tify\Recipient\AbstractRecipient; |
13
|
|
|
use Jgut\Tify\Message\AbstractMessage; |
14
|
|
|
use Jgut\Tify\OptionsTrait; |
15
|
|
|
use Jgut\Tify\Service\AbstractService; |
16
|
|
|
|
17
|
|
|
abstract class AbstractNotification |
18
|
|
|
{ |
19
|
|
|
const STATUS_PENDING = 0; |
20
|
|
|
const STATUS_SENT = 1; |
21
|
|
|
|
22
|
|
|
use OptionsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default notification options. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $defaultOptions = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Jgut\Tify\Service\AbstractService |
33
|
|
|
*/ |
34
|
|
|
protected $service; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Jgut\Tify\Message\AbstractMessage |
38
|
|
|
*/ |
39
|
|
|
protected $message; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Jgut\Tify\Recipient\AbstractRecipient[] |
43
|
|
|
*/ |
44
|
|
|
protected $recipients = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $status = self::STATUS_PENDING; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Notification resultss. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $results = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Jgut\Tify\Service\AbstractService $service |
60
|
|
|
* @param \Jgut\Tify\Message\AbstractMessage $message |
61
|
|
|
* @param \Jgut\Tify\Recipient\AbstractRecipient[] $recipients |
62
|
|
|
* @param array $options |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
AbstractService $service, |
66
|
|
|
AbstractMessage $message, |
67
|
|
|
array $recipients = [], |
68
|
|
|
array $options = [] |
69
|
|
|
) { |
70
|
|
|
$this->service = $service; |
71
|
|
|
$this->message = $message; |
72
|
|
|
|
73
|
|
|
foreach ($recipients as $recipient) { |
74
|
|
|
$this->addRecipient($recipient); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->setOptions(array_merge($this->defaultOptions, $options)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get service. |
82
|
|
|
* |
83
|
|
|
* @return \Jgut\Tify\Service\AbstractService |
84
|
|
|
*/ |
85
|
|
|
final public function getService() |
86
|
|
|
{ |
87
|
|
|
return $this->service; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set service. |
92
|
|
|
* |
93
|
|
|
* @param \Jgut\Tify\Service\AbstractService $service |
94
|
|
|
*/ |
95
|
|
|
abstract public function setService(AbstractService $service); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get message. |
99
|
|
|
* |
100
|
|
|
* @return \Jgut\Tify\Message\AbstractMessage |
101
|
|
|
*/ |
102
|
|
|
final public function getMessage() |
103
|
|
|
{ |
104
|
|
|
return $this->message; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set message. |
109
|
|
|
* |
110
|
|
|
* @param \Jgut\Tify\Message\AbstractMessage $message |
111
|
|
|
*/ |
112
|
|
|
abstract public function setMessage(AbstractMessage $message); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrieve list of recipients. |
116
|
|
|
* |
117
|
|
|
* @return \Jgut\Tify\Recipient\AbstractRecipient[] |
118
|
|
|
*/ |
119
|
|
|
final public function getRecipients() |
120
|
|
|
{ |
121
|
|
|
return $this->recipients; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add recipient. |
126
|
|
|
* |
127
|
|
|
* @param \Jgut\Tify\Recipient\AbstractRecipient $recipient |
128
|
|
|
*/ |
129
|
|
|
abstract public function addRecipient(AbstractRecipient $recipient); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieve notification status. |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
final public function getStatus() |
137
|
|
|
{ |
138
|
|
|
return $this->status; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check if notification status is pushed. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
final public function isSent() |
147
|
|
|
{ |
148
|
|
|
return $this->status === static::STATUS_SENT; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set notification as sent. |
153
|
|
|
* |
154
|
|
|
* @param array $results |
155
|
|
|
*/ |
156
|
|
|
final public function setSent(array $results = []) |
157
|
|
|
{ |
158
|
|
|
$this->status = static::STATUS_SENT; |
159
|
|
|
$this->results = $results; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set notification pending (not sent). |
164
|
|
|
*/ |
165
|
|
|
final public function setPending() |
166
|
|
|
{ |
167
|
|
|
$this->status = static::STATUS_PENDING; |
168
|
|
|
$this->results = []; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Retrieve results. |
173
|
|
|
* |
174
|
|
|
* @return \Jgut\Tify\Result[] |
175
|
|
|
*/ |
176
|
|
|
final public function getResults() |
177
|
|
|
{ |
178
|
|
|
return $this->results; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retrieve recipients tokens list. |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
final public function getTokens() |
187
|
|
|
{ |
188
|
|
|
$tokens = []; |
189
|
|
|
|
190
|
|
|
foreach ($this->recipients as $recipient) { |
191
|
|
|
$tokens[] = $recipient->getToken(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return array_unique(array_filter($tokens)); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|