|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 14.10.18 |
|
6
|
|
|
* Time: 19:50 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Notifications; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Foundation\Channels\DatabaseNotificationChannel; |
|
13
|
|
|
use Foundation\Channels\WebNotificationChannel; |
|
14
|
|
|
use Illuminate\Notifications\Notification; |
|
15
|
|
|
|
|
16
|
|
|
abstract class WebNotification extends Notification |
|
17
|
|
|
{ |
|
18
|
|
|
protected $targetModel; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* WebNotification constructor. |
|
22
|
|
|
* @param $targetModel |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct($targetModel) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->targetModel = $targetModel; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the array representation of the notification. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $notifiable |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function toDatabase($notifiable) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'title' => $this->title(), |
|
39
|
|
|
'message' => $this->message(), |
|
40
|
|
|
'target' => get_short_class_name($this->targetModel), |
|
41
|
|
|
'target_id' => $this->targetModel->getKey(), |
|
42
|
|
|
'tag' => $this->tag(), |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $notifiable |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public function toBroadcast($notifiable) |
|
51
|
|
|
{ |
|
52
|
|
|
$notification = $notifiable->unreadNotifications->last(); |
|
53
|
|
|
return [ |
|
54
|
|
|
'id' => $notification->getKey(), |
|
55
|
|
|
'target_id' => $this->targetModel->getKey(), |
|
56
|
|
|
'target' => get_short_class_name($this->targetModel), |
|
57
|
|
|
'tag' => $this->tag(), |
|
58
|
|
|
'title' => $this->title(), |
|
59
|
|
|
'message' => $this->message(), |
|
60
|
|
|
'is_read' => isset($notification->read_at) ? true : false |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The title for the web notification |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract protected function title(): string; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The message for the web notification |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract protected function message(): string; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The tag for the web notification |
|
78
|
|
|
* success | info | warning | danger |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function tag() |
|
82
|
|
|
{ |
|
83
|
|
|
return 'info'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Do not change the order database must be called before broadcast. |
|
88
|
|
|
* Otherwise we cannot get the appropriate id to broadcast |
|
89
|
|
|
* @param $notifiable |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function via($notifiable) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
return [ |
|
95
|
|
|
DatabaseNotificationChannel::class, |
|
96
|
|
|
WebNotificationChannel::class |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.