|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Listeners; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Events\JobDeleted; |
|
6
|
|
|
use Coyote\Events\MicroblogWasDeleted; |
|
7
|
|
|
use Coyote\Events\MicroblogWasSaved; |
|
8
|
|
|
use Coyote\Events\TopicWasMoved; |
|
9
|
|
|
use Coyote\Events\TopicWasSaved; |
|
10
|
|
|
use Coyote\Events\TopicWasDeleted; |
|
11
|
|
|
use Coyote\Events\ForumWasSaved; |
|
12
|
|
|
use Coyote\Events\ForumWasDeleted; |
|
13
|
|
|
use Coyote\Events\JobWasSaved; |
|
14
|
|
|
use Coyote\Events\WikiWasDeleted; |
|
15
|
|
|
use Coyote\Events\WikiWasSaved; |
|
16
|
|
|
use Coyote\Microblog; |
|
17
|
|
|
use Coyote\Job; |
|
18
|
|
|
use Coyote\Services\UrlBuilder\UrlBuilder; |
|
19
|
|
|
use Coyote\Topic; |
|
20
|
|
|
use Coyote\Forum; |
|
21
|
|
|
use Coyote\Repositories\Contracts\PageRepositoryInterface as PageRepository; |
|
22
|
|
|
use Coyote\Wiki; |
|
23
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
24
|
|
|
use Illuminate\Contracts\Console\Kernel; |
|
25
|
|
|
|
|
26
|
|
|
class PageListener implements ShouldQueue |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var PageRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $page; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Kernel |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $kernel; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param PageRepository $page |
|
40
|
|
|
* @param Kernel $kernel |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(PageRepository $page, Kernel $kernel) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->page = $page; |
|
45
|
|
|
$this->kernel = $kernel; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param TopicWasSaved $event |
|
50
|
|
|
*/ |
|
51
|
|
|
public function onTopicSave(TopicWasSaved $event) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->updateTopic($event->topic); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param TopicWasMoved $event |
|
58
|
|
|
*/ |
|
59
|
|
|
public function onTopicMove(TopicWasMoved $event) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->updateTopic($event->topic); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Topic $topic |
|
66
|
|
|
*/ |
|
67
|
|
|
private function updateTopic(Topic $topic) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->purgePageViews(); |
|
70
|
|
|
|
|
71
|
|
|
$topic->page()->updateOrCreate([ |
|
72
|
|
|
'content_id' => $topic->id, |
|
73
|
|
|
'content_type' => Topic::class |
|
74
|
|
|
], [ |
|
75
|
|
|
'title' => $topic->subject, |
|
76
|
|
|
'tags' => $topic->tags->pluck('name'), |
|
77
|
|
|
'path' => UrlBuilder::topic($topic), |
|
78
|
|
|
'allow_sitemap' => !$topic->forum->access()->exists() |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param TopicWasDeleted $event |
|
84
|
|
|
*/ |
|
85
|
|
|
public function onTopicDelete(TopicWasDeleted $event) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->page->findByContent($event->topic['id'], Topic::class)->delete(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param ForumWasSaved $event |
|
92
|
|
|
*/ |
|
93
|
|
|
public function onForumSave(ForumWasSaved $event) |
|
94
|
|
|
{ |
|
95
|
|
|
$event->forum->page()->updateOrCreate([ |
|
96
|
|
|
'content_id' => $event->forum->id, |
|
97
|
|
|
'content_type' => Forum::class |
|
98
|
|
|
], [ |
|
99
|
|
|
'title' => $event->forum->name, |
|
100
|
|
|
'path' => UrlBuilder::forum($event->forum) |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param ForumWasDeleted $event |
|
106
|
|
|
*/ |
|
107
|
|
|
public function onForumDelete(ForumWasDeleted $event) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->page->findByContent($event->forum['id'], Forum::class)->delete(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param MicroblogWasSaved $event |
|
114
|
|
|
*/ |
|
115
|
|
|
public function onMicroblogSave(MicroblogWasSaved $event) |
|
116
|
|
|
{ |
|
117
|
|
|
$event->microblog->page()->updateOrCreate([ |
|
118
|
|
|
'content_id' => $event->microblog->id, |
|
119
|
|
|
'content_type' => Microblog::class, |
|
120
|
|
|
], [ |
|
121
|
|
|
'title' => excerpt($event->microblog->html, 28), |
|
122
|
|
|
'path' => UrlBuilder::microblog($event->microblog), |
|
123
|
|
|
'tags' => $event->microblog->tags->pluck('name') |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param MicroblogWasDeleted $event |
|
129
|
|
|
*/ |
|
130
|
|
|
public function onMicroblogDelete(MicroblogWasDeleted $event) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->page->findByContent($event->microblog['id'], Microblog::class)->delete(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param JobWasSaved $event |
|
137
|
|
|
*/ |
|
138
|
|
|
public function onJobSave(JobWasSaved $event) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->purgePageViews(); |
|
141
|
|
|
|
|
142
|
|
|
$event->job->page()->updateOrCreate([ |
|
143
|
|
|
'content_id' => $event->job->id, |
|
144
|
|
|
'content_type' => Job::class, |
|
145
|
|
|
], [ |
|
146
|
|
|
'title' => $event->job->title, |
|
147
|
|
|
'tags' => $event->job->tags->pluck('name'), |
|
148
|
|
|
'path' => UrlBuilder::job($event->job) |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param JobDeleted $event |
|
154
|
|
|
*/ |
|
155
|
|
|
public function onJobDelete(JobDeleted $event) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->page->findByContent($event->job['id'], Job::class)->delete(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param WikiWasSaved $event |
|
162
|
|
|
*/ |
|
163
|
|
|
public function onWikiSave(WikiWasSaved $event) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->purgePageViews(); |
|
166
|
|
|
|
|
167
|
|
|
$event->wiki->page()->updateOrCreate([ |
|
168
|
|
|
'content_id' => $event->wiki->id, |
|
169
|
|
|
'content_type' => Wiki::class, |
|
170
|
|
|
], [ |
|
171
|
|
|
'title' => $event->wiki->title, |
|
172
|
|
|
'path' => UrlBuilder::wiki($event->wiki) |
|
173
|
|
|
]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param WikiWasDeleted $event |
|
178
|
|
|
*/ |
|
179
|
|
|
public function onWikiDelete(WikiWasDeleted $event) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->page->findByContent($event->wiki['id'], Wiki::class)->delete(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Register the listeners for the subscriber. |
|
186
|
|
|
* |
|
187
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
|
188
|
|
|
*/ |
|
189
|
|
|
public function subscribe($events) |
|
190
|
|
|
{ |
|
191
|
|
|
$events->listen( |
|
192
|
|
|
'Coyote\Events\TopicWasSaved', |
|
193
|
|
|
'Coyote\Listeners\PageListener@onTopicSave' |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
$events->listen( |
|
197
|
|
|
'Coyote\Events\TopicWasMoved', |
|
198
|
|
|
'Coyote\Listeners\PageListener@onTopicMove' |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
$events->listen( |
|
202
|
|
|
'Coyote\Events\TopicWasDeleted', |
|
203
|
|
|
'Coyote\Listeners\PageListener@onTopicDelete' |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
$events->listen( |
|
207
|
|
|
'Coyote\Events\ForumWasSaved', |
|
208
|
|
|
'Coyote\Listeners\PageListener@onForumSave' |
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
$events->listen( |
|
212
|
|
|
'Coyote\Events\ForumWasDeleted', |
|
213
|
|
|
'Coyote\Listeners\PageListener@onForumDelete' |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$events->listen( |
|
217
|
|
|
'Coyote\Events\MicroblogWasSaved', |
|
218
|
|
|
'Coyote\Listeners\PageListener@onMicroblogSave' |
|
219
|
|
|
); |
|
220
|
|
|
|
|
221
|
|
|
$events->listen( |
|
222
|
|
|
'Coyote\Events\MicroblogWasDeleted', |
|
223
|
|
|
'Coyote\Listeners\PageListener@onMicroblogDelete' |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
$events->listen( |
|
227
|
|
|
'Coyote\Events\JobWasSaved', |
|
228
|
|
|
'Coyote\Listeners\PageListener@onJobSave' |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
$events->listen( |
|
232
|
|
|
'Coyote\Events\JobDeleted', |
|
233
|
|
|
'Coyote\Listeners\PageListener@onJobDelete' |
|
234
|
|
|
); |
|
235
|
|
|
|
|
236
|
|
|
$events->listen( |
|
237
|
|
|
'Coyote\Events\WikiWasSaved', |
|
238
|
|
|
'Coyote\Listeners\PageListener@onWikiSave' |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
$events->listen( |
|
242
|
|
|
'Coyote\Events\WikiWasDeleted', |
|
243
|
|
|
'Coyote\Listeners\PageListener@onWikiDelete' |
|
244
|
|
|
); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
// before changing page path we MUST save page views that are stored in redis |
|
248
|
|
|
private function purgePageViews() |
|
249
|
|
|
{ |
|
250
|
|
|
if (!app()->environment('local', 'dev')) { |
|
|
|
|
|
|
251
|
|
|
$this->kernel->call('coyote:counter'); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|