1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Listeners\Subscribers; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Xetaravel\Events\RegisterEvent; |
7
|
|
|
use Xetaravel\Events\CommentEvent; |
8
|
|
|
use Xetaravel\Models\Badge; |
9
|
|
|
use Xetaravel\Models\User; |
10
|
|
|
use Xetaravel\Notifications\BadgeNotification; |
11
|
|
|
|
12
|
|
|
class BadgeSubscriber |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Register the listeners for the subscriber. |
16
|
|
|
* |
17
|
|
|
* @param Illuminate\Events\Dispatcher $events |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function subscribe($events) |
22
|
|
|
{ |
23
|
|
|
$events->listen( |
24
|
|
|
'Xetaravel\Events\RegisterEvent', |
25
|
|
|
'Xetaravel\Listeners\Subscribers\BadgeSubscriber@onNewRegister' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$events->listen( |
29
|
|
|
'Xetaravel\Events\CommentEvent', |
30
|
|
|
'Xetaravel\Listeners\Subscribers\BadgeSubscriber@onNewComment' |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Listener related to the comment badge. |
36
|
|
|
* |
37
|
|
|
* @param \Xetaravel\Events\CommentEvent $event The event that was fired. |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function onNewComment(CommentEvent $event): bool |
42
|
|
|
{ |
43
|
|
|
$user = $event->user; |
44
|
|
|
$badges = Badge::where('type', 'onNewComment')->get(); |
45
|
|
|
|
46
|
|
|
$collection = $badges->filter(function ($badge) use ($user) { |
47
|
|
|
return $badge->rule <= $user->comment_count; |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$result = $user->badges()->syncWithoutDetaching($collection); |
51
|
|
|
|
52
|
|
|
return $this->sendNotifications($result, $badges, $user); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Listener related to the register badge. |
57
|
|
|
* |
58
|
|
|
* @param \Xetaravel\Events\RegisterEvent $event The event that was fired. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function onNewRegister(RegisterEvent $event): bool |
63
|
|
|
{ |
64
|
|
|
$user = $event->user; |
65
|
|
|
$badges = Badge::where('type', 'onNewRegister')->get(); |
66
|
|
|
|
67
|
|
|
$today = new Carbon(); |
68
|
|
|
$diff = $today->diff($user->created_at)->y; |
69
|
|
|
|
70
|
|
|
$collection = $badges->filter(function ($badge) use ($diff) { |
71
|
|
|
return $badge->rule <= $diff; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$result = $user->badges()->syncWithoutDetaching($collection); |
75
|
|
|
|
76
|
|
|
return $this->sendNotifications($result, $badges, $user); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Send a notification for each new badge unlocked. |
81
|
|
|
* |
82
|
|
|
* @param array $result The result of the synchronization. |
83
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $badges The badges collection related to the listener. |
84
|
|
|
* @param \Xetaravel\Models\User $user The user to nitify. |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
protected function sendNotifications(array $result, Collection $badges, User $user): bool |
89
|
|
|
{ |
90
|
|
|
if (empty($result['attached'])) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$sendNotification = function ($badgeId, $key, $badges) use ($user) { |
95
|
|
|
$badgeCollection = $badges->filter(function ($badge) use ($badgeId) { |
96
|
|
|
return $badge->id == $badgeId; |
97
|
|
|
})->first(); |
98
|
|
|
|
99
|
|
|
$user->notify(new BadgeNotification($badgeCollection)); |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
return array_walk($result['attached'], $sendNotification, $badges); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|