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