Passed
Branch master (cd4548)
by Fèvre
19:36
created

BadgeSubscriber::onNewExperiences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Xetaravel\Listeners\Subscribers;
3
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Collection;
6
use Xetaravel\Events\Badges\ExperiencesEvent;
7
use Xetaravel\Events\Badges\PostEvent;
8
use Xetaravel\Events\Badges\PostSolvedEvent;
9
use Xetaravel\Events\RegisterEvent;
0 ignored issues
show
Bug introduced by
The type Xetaravel\Events\RegisterEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Xetaravel\Events\CommentEvent;
0 ignored issues
show
Bug introduced by
The type Xetaravel\Events\CommentEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Xetaravel\Models\Badge;
12
use Xetaravel\Models\DiscussPost;
13
use Xetaravel\Models\User;
14
use Xetaravel\Notifications\BadgeNotification;
15
16
class BadgeSubscriber
17
{
18
    /**
19
     * The events mapping to the listener function.
20
     *
21
     * @var array
22
     */
23
    protected $events = [
24
        RegisterEvent::class => 'onNewRegister',
25
        PostEvent::class => 'onNewPost',
26
        PostSolvedEvent::class => 'onNewPostSolved',
27
        ExperiencesEvent::class => 'onNewExperiences'
28
    ];
29
30
    /**
31
     * Register the listeners for the subscriber.
32
     *
33
     * @param Illuminate\Events\Dispatcher $events
0 ignored issues
show
Bug introduced by
The type Xetaravel\Listeners\Subs...inate\Events\Dispatcher was not found. Did you mean Illuminate\Events\Dispatcher? If so, make sure to prefix the type with \.
Loading history...
34
     *
35
     * @return void
36
     */
37
    public function subscribe($events)
38
    {
39
        foreach ($this->events as $event => $action) {
40
            $events->listen($event, BadgeSubscriber::class . '@' . $action);
41
        }
42
    }
43
44
    /**
45
     * Listener related to the comment badge.
46
     *
47
     * @param \Xetaravel\Events\CommentEvent $event The event that was fired.
48
     *
49
     * @return bool
50
     */
51
    public function onNewComment(CommentEvent $event): bool
52
    {
53
        $user = $event->user;
54
        $badges = Badge::where('type', 'onNewComment')->get();
55
56
        $collection = $badges->filter(function ($badge) use ($user) {
57
            return $badge->rule <= $user->comment_count;
58
        });
59
60
        $result = $user->badges()->syncWithoutDetaching($collection);
61
62
        return $this->sendNotifications($result, $badges, $user);
63
    }
64
65
    /**
66
     * Listener related to the posts badge.
67
     *
68
     * @param \Xetaravel\Events\PostEvent $event The event that was fired.
0 ignored issues
show
Bug introduced by
The type Xetaravel\Events\PostEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
     *
70
     * @return bool
71
     */
72
    public function onNewPost(PostEvent $event): bool
73
    {
74
        $user = $event->user;
75
        $badges = Badge::where('type', 'onNewPost')->get();
76
77
        $collection = $badges->filter(function ($badge) use ($user) {
78
            return $badge->rule <= $user->discuss_post_count;
79
        });
80
81
        $result = $user->badges()->syncWithoutDetaching($collection);
82
83
        return $this->sendNotifications($result, $badges, $user);
84
    }
85
86
    /**
87
     * Listener related to the post solved badge.
88
     *
89
     * @param \Xetaravel\Events\PostSolvedEvent $event The event that was fired.
0 ignored issues
show
Bug introduced by
The type Xetaravel\Events\PostSolvedEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
90
     *
91
     * @return bool
92
     */
93
    public function onNewPostSolved(PostSolvedEvent $event): bool
94
    {
95
        $user = $event->user;
96
        $badges = Badge::where('type', 'onNewPostSolved')->get();
97
98
        $collection = $badges->filter(function ($badge) use ($user) {
99
            $postsSolved = DiscussPost::where('user_id', $user->id)
100
                    ->where('is_solved', true)
101
                    ->count();
102
103
            return $badge->rule <= $postsSolved;
104
        });
105
106
        $result = $user->badges()->syncWithoutDetaching($collection);
107
108
        return $this->sendNotifications($result, $badges, $user);
109
    }
110
111
    /**
112
     * Listener related to the experiences badge.
113
     *
114
     * @param \Xetaravel\Events\ExperiencesEvent $event The event that was fired.
0 ignored issues
show
Bug introduced by
The type Xetaravel\Events\ExperiencesEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
     *
116
     * @return bool
117
     */
118
    public function onNewExperiences(ExperiencesEvent $event): bool
119
    {
120
        $user = $event->user;
121
        $badges = Badge::where('type', 'onNewExperiences')->get();
122
123
        $collection = $badges->filter(function ($badge) use ($user) {
124
            return $badge->rule <= $user->experiences_total;
125
        });
126
127
        $result = $user->badges()->syncWithoutDetaching($collection);
128
129
        return $this->sendNotifications($result, $badges, $user);
130
    }
131
132
    /**
133
     * Listener related to the register badge.
134
     *
135
     * @param \Xetaravel\Events\RegisterEvent $event The event that was fired.
136
     *
137
     * @return bool
138
     */
139
    public function onNewRegister(RegisterEvent $event): bool
140
    {
141
        $user = $event->user;
142
        $badges = Badge::where('type', 'onNewRegister')->get();
143
144
        $today = new Carbon();
145
        $diff = $today->diff($user->created_at)->y;
146
147
        $collection = $badges->filter(function ($badge) use ($diff) {
148
            return $badge->rule <= $diff;
149
        });
150
151
        $result = $user->badges()->syncWithoutDetaching($collection);
152
153
        return $this->sendNotifications($result, $badges, $user);
154
    }
155
156
    /**
157
     * Send a notification for each new badge unlocked.
158
     *
159
     * @param array $result The result of the synchronization.
160
     * @param \Illuminate\Database\Eloquent\Collection $badges The badges collection related to the listener.
161
     * @param \Xetaravel\Models\User $user The user to notify.
162
     *
163
     * @return bool
164
     */
165
    protected function sendNotifications(array $result, Collection $badges, User $user): bool
166
    {
167
        if (empty($result['attached'])) {
168
            return true;
169
        }
170
171
        $sendNotification = function ($badgeId, $key, $badges) use ($user) {
172
            $badgeCollection = $badges->filter(function ($badge) use ($badgeId) {
173
                return $badge->id == $badgeId;
174
            })->first();
175
176
            $user->notify(new BadgeNotification($badgeCollection));
177
        };
178
179
        return array_walk($result['attached'], $sendNotification, $badges);
180
    }
181
}
182