|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Notifications; |
|
6
|
|
|
|
|
7
|
|
|
use Baka\Mail\Message; |
|
|
|
|
|
|
8
|
|
|
use Canvas\Contracts\Auth\UserInterface; |
|
9
|
|
|
use Canvas\Contracts\Notifications\NotificationInterface; |
|
10
|
|
|
use Canvas\Models\AbstractModel; |
|
11
|
|
|
use Canvas\Models\Notifications; |
|
12
|
|
|
use Canvas\Models\NotificationType; |
|
13
|
|
|
use Canvas\Models\Users; |
|
|
|
|
|
|
14
|
|
|
use Canvas\Queue\Queue; |
|
15
|
|
|
use Phalcon\Di; |
|
16
|
|
|
use Phalcon\Mvc\Model; |
|
|
|
|
|
|
17
|
|
|
use Phalcon\Traits\EventManagerAwareTrait; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Notification implements NotificationInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use EventManagerAwareTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var Users |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $toUser = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var Users |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fromUser = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Send this notification to the queue? |
|
37
|
|
|
* |
|
38
|
|
|
* @var boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $useQueue = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @var NotificationType |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $type = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* |
|
50
|
|
|
* @var AbstractModel |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $entity = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
* @var Baka\Mail\Manager |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
protected $mail; |
|
59
|
|
|
|
|
60
|
|
|
const USERS = 'Canvas\Notifications\Users'; |
|
61
|
|
|
const SYSTEM = 'Canvas\Notifications\System'; |
|
62
|
|
|
const APPS = 'Canvas\Notifications\Apps'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor. |
|
66
|
|
|
* |
|
67
|
|
|
* @param AbstractModel $entity |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(Model $entity) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->entity = $entity; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set the notification type. |
|
76
|
|
|
* |
|
77
|
|
|
* @param NotificationType $type |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setType(NotificationType $type): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->type = $type; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return the message from the current notification type. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function message(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->type->template ?: ''; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Define a Baka Mail to send a email. |
|
98
|
|
|
* |
|
99
|
|
|
* @todo add Interfase to bakaMail |
|
100
|
|
|
* |
|
101
|
|
|
* @return Message |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function toMail(): ?Message |
|
104
|
|
|
{ |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* To send push notification. |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function toPushNotification(): ?PushNotification |
|
114
|
|
|
{ |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Send to websocket / realtime. |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function toRealtime(): ?PusherNotification |
|
124
|
|
|
{ |
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the usre we are sending the notification to. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Users $user |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setTo(UserInterface $user): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->toUser = $user; |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the user from who the notification if comming from. |
|
142
|
|
|
* |
|
143
|
|
|
* @param User $user |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setFrom(UserInterface $user): void |
|
148
|
|
|
{ |
|
149
|
|
|
$this->fromUser = $user; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Set the usre we are sending the notification to. |
|
154
|
|
|
* |
|
155
|
|
|
* @return Users |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getTo(): ?UserInterface |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->toUser; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* get the user from who the notification if comming from. |
|
164
|
|
|
* |
|
165
|
|
|
* @return User |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getFrom(): ?UserInterface |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->fromUser; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Disable this notification queue in runtime. |
|
174
|
|
|
* |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
|
|
public function disableQueue(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$this->useQueue = false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Process the notification |
|
184
|
|
|
* - handle the db |
|
185
|
|
|
* - trigger the notification |
|
186
|
|
|
* - knows if we have to send it to queu. |
|
187
|
|
|
* |
|
188
|
|
|
* @return boolean |
|
189
|
|
|
*/ |
|
190
|
|
|
public function process(): bool |
|
191
|
|
|
{ |
|
192
|
|
|
//if the user didnt provide the type get it based on the class name |
|
193
|
|
|
if (is_null($this->type)) { |
|
194
|
|
|
$this->setType(NotificationType::getByKey(static::class)); |
|
195
|
|
|
} elseif (is_string($this->type)) { |
|
|
|
|
|
|
196
|
|
|
//not great but for now lets use it |
|
197
|
|
|
$this->setType(NotificationType::getByKey($this->type)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (Di::getDefault()->has('mail')) { |
|
201
|
|
|
$this->mail = Di::getDefault()->getMail(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
if ($this->useQueue) { |
|
205
|
|
|
$this->sendToQueue(); |
|
206
|
|
|
return true; //send it to the queue |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$this->trigger(); |
|
210
|
|
|
|
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Send to our internal Notification queue. |
|
216
|
|
|
* |
|
217
|
|
|
* @return boolean |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function sendToQueue(): bool |
|
220
|
|
|
{ |
|
221
|
|
|
$notificationData = [ |
|
222
|
|
|
'from' => $this->fromUser, |
|
223
|
|
|
'to' => $this->toUser, |
|
224
|
|
|
'entity' => $this->entity, |
|
225
|
|
|
'type' => $this->type, |
|
226
|
|
|
'notification' => get_class($this), |
|
227
|
|
|
]; |
|
228
|
|
|
|
|
229
|
|
|
return Queue::send(Queue::NOTIFICATIONS, serialize($notificationData)); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Send the noficiatino to the places the user defined. |
|
234
|
|
|
* |
|
235
|
|
|
* @return boolean |
|
236
|
|
|
*/ |
|
237
|
|
|
public function trigger(): bool |
|
238
|
|
|
{ |
|
239
|
|
|
$content = $this->message(); |
|
240
|
|
|
$app = Di::getDefault()->getApp(); |
|
241
|
|
|
|
|
242
|
|
|
//save to DB |
|
243
|
|
|
$notification = new Notifications(); |
|
244
|
|
|
$notification->from_users_id = $this->fromUser->getId(); |
|
245
|
|
|
$notification->users_id = $this->toUser->getId(); |
|
246
|
|
|
$notification->companies_id = $this->fromUser->currentCompanyId(); |
|
247
|
|
|
$notification->apps_id = $app->getId(); |
|
248
|
|
|
$notification->system_modules_id = $this->type->system_modules_id; |
|
249
|
|
|
$notification->notification_type_id = $this->type->getId(); |
|
250
|
|
|
$notification->entity_id = $this->entity->getId(); |
|
251
|
|
|
$notification->content = $content; |
|
252
|
|
|
$notification->read = 0; |
|
253
|
|
|
$notification->saveOrFail(); |
|
254
|
|
|
|
|
255
|
|
|
$toMail = $this->toMail(); |
|
|
|
|
|
|
256
|
|
|
if ($toMail instanceof Message) { |
|
257
|
|
|
$this->fire('notification:sendMail', $toMail); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$toPushNotification = $this->toPushNotification(); |
|
|
|
|
|
|
261
|
|
|
if ($toPushNotification instanceof PushNotification) { |
|
262
|
|
|
$this->fire('notification:sendPushNotification', $toPushNotification); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
$toRealtime = $this->toRealtime(); |
|
|
|
|
|
|
266
|
|
|
if ($toRealtime instanceof PusherNotification) { |
|
267
|
|
|
$this->fire('notification:sendRealtimeNotification', $toRealtime); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @todo send to push ontification |
|
272
|
|
|
*/ |
|
273
|
|
|
|
|
274
|
|
|
if ($this->type->with_realtime) { |
|
275
|
|
|
$this->fire('notification:sendRealtime', $this); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return true; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths