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