|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Listeners\Subscribers\Discuss; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Events\Dispatcher; |
|
8
|
|
|
use Xetaravel\Events\Discuss\CategoryWasChangedEvent; |
|
9
|
|
|
use Xetaravel\Events\Discuss\ConversationWasLockedEvent; |
|
10
|
|
|
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent; |
|
11
|
|
|
use Xetaravel\Events\Discuss\PostWasDeletedEvent; |
|
12
|
|
|
use Xetaravel\Events\Discuss\TitleWasChangedEvent; |
|
13
|
|
|
use Xetaravel\Models\DiscussLog; |
|
14
|
|
|
|
|
15
|
|
|
class LogSubscriber |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The events mapping to the listener function. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected array $events = [ |
|
23
|
|
|
CategoryWasChangedEvent::class => 'handleCategoryWasChanged', |
|
24
|
|
|
ConversationWasLockedEvent::class => 'handleConversationWasLocked', |
|
25
|
|
|
ConversationWasPinnedEvent::class => 'handleConversationWasPinned', |
|
26
|
|
|
PostWasDeletedEvent::class => 'handlePostWasDeleted', |
|
27
|
|
|
TitleWasChangedEvent::class => 'handleTitleWasChanged' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create the log. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $data The data used to create the log. |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function create(array $data): bool |
|
38
|
|
|
{ |
|
39
|
|
|
if (!isset($data['data'])) { |
|
40
|
|
|
$data['data'] = []; |
|
41
|
|
|
} |
|
42
|
|
|
$log = DiscussLog::create($data); |
|
43
|
|
|
|
|
44
|
|
|
return !(is_null($log)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Register the listeners for the subscriber. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Dispatcher $events |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function subscribe(Dispatcher $events): void |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($this->events as $event => $action) { |
|
57
|
|
|
$events->listen($event, self::class . '@' . $action); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handle a CategoryWasChanged event. |
|
63
|
|
|
* |
|
64
|
|
|
* @param CategoryWasChangedEvent $event The event that was fired. |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function handleCategoryWasChanged(CategoryWasChangedEvent $event): bool |
|
69
|
|
|
{ |
|
70
|
|
|
$data = [ |
|
71
|
|
|
'loggable_id' => $event->discussConversation->getKey(), |
|
72
|
|
|
'loggable_type' => get_class($event->discussConversation), |
|
73
|
|
|
'event_type' => CategoryWasChangedEvent::class, |
|
74
|
|
|
'data' => [ |
|
75
|
|
|
'old' => $event->oldCategory, |
|
76
|
|
|
'new' => $event->category |
|
77
|
|
|
] |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
return $this->create($data); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Handle a ConversationWasLocked event. |
|
85
|
|
|
* |
|
86
|
|
|
* @param ConversationWasLockedEvent $event The event that was fired. |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function handleConversationWasLocked(ConversationWasLockedEvent $event): bool |
|
91
|
|
|
{ |
|
92
|
|
|
$data = [ |
|
93
|
|
|
'loggable_id' => $event->discussConversation->getKey(), |
|
94
|
|
|
'loggable_type' => get_class($event->discussConversation), |
|
95
|
|
|
'event_type' => ConversationWasLockedEvent::class |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
return $this->create($data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Handle a ConversationWasPinned event. |
|
103
|
|
|
* |
|
104
|
|
|
* @param ConversationWasPinnedEvent $event The event that was fired. |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function handleConversationWasPinned(ConversationWasPinnedEvent $event): bool |
|
109
|
|
|
{ |
|
110
|
|
|
$data = [ |
|
111
|
|
|
'loggable_id' => $event->discussConversation->getKey(), |
|
112
|
|
|
'loggable_type' => get_class($event->discussConversation), |
|
113
|
|
|
'event_type' => ConversationWasPinnedEvent::class |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
return $this->create($data); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Handle a PostWasDeleted event. |
|
121
|
|
|
* |
|
122
|
|
|
* @param PostWasDeletedEvent $event The event that was fired. |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function handlePostWasDeleted(PostWasDeletedEvent $event): bool |
|
127
|
|
|
{ |
|
128
|
|
|
$data = [ |
|
129
|
|
|
'loggable_id' => $event->discussConversation->getKey(), |
|
130
|
|
|
'loggable_type' => get_class($event->discussConversation), |
|
131
|
|
|
'event_type' => PostWasDeletedEvent::class, |
|
132
|
|
|
'data' => [ |
|
133
|
|
|
'post_user_id' => $event->user->getKey() |
|
134
|
|
|
] |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
return $this->create($data); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Handle a TitleWasChanged event. |
|
142
|
|
|
* |
|
143
|
|
|
* @param TitleWasChangedEvent $event The event that was fired. |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function handleTitleWasChanged(TitleWasChangedEvent $event): bool |
|
148
|
|
|
{ |
|
149
|
|
|
$data = [ |
|
150
|
|
|
'loggable_id' => $event->discussConversation->getKey(), |
|
151
|
|
|
'loggable_type' => get_class($event->discussConversation), |
|
152
|
|
|
'event_type' => TitleWasChangedEvent::class, |
|
153
|
|
|
'data' => [ |
|
154
|
|
|
'old' => $event->oldTitle, |
|
155
|
|
|
'new' => $event->title |
|
156
|
|
|
] |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
return $this->create($data); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|