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