/
UserMetadataUpdater.php
83 lines (72 loc) · 2.14 KB
/
UserMetadataUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
use Flarum\Discussion\Event\Started;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Post\Event\Posted;
use Illuminate\Contracts\Events\Dispatcher;
class UserMetadataUpdater
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
$events->listen(PostDeleted::class, [$this, 'whenPostWasDeleted']);
$events->listen(Started::class, [$this, 'whenDiscussionWasStarted']);
$events->listen(DiscussionDeleted::class, [$this, 'whenDiscussionWasDeleted']);
}
/**
* @param \Flarum\Post\Event\Posted $event
*/
public function whenPostWasPosted(Posted $event)
{
$this->updateCommentsCount($event->post->user);
}
/**
* @param \Flarum\Post\Event\Deleted $event
*/
public function whenPostWasDeleted(PostDeleted $event)
{
$this->updateCommentsCount($event->post->user);
}
/**
* @param \Flarum\Discussion\Event\Started $event
*/
public function whenDiscussionWasStarted(Started $event)
{
$this->updateDiscussionsCount($event->discussion);
}
/**
* @param \Flarum\Discussion\Event\Deleted $event
*/
public function whenDiscussionWasDeleted(DiscussionDeleted $event)
{
$this->updateDiscussionsCount($event->discussion);
$this->updateCommentsCount($event->discussion->user);
}
/**
* @param \Flarum\User\User $user
*/
private function updateCommentsCount(?User $user)
{
if ($user && $user->exists) {
$user->refreshCommentCount()->save();
}
}
private function updateDiscussionsCount(Discussion $discussion)
{
$user = $discussion->user;
if ($user && $user->exists) {
$user->refreshDiscussionCount()->save();
}
}
}