Passed
Push — Leaderboard ( 298845 )
by Fèvre
06:39
created

Leaderboard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
namespace Xetaravel\Console\Commands;
3
4
use Illuminate\Console\Command;
5
use Illuminate\Database\Eloquent\Builder;
6
use Xetaravel\Events\Badges\LeaderboardEvent;
7
use Xetaravel\Models\User;
8
9
class Leaderboard extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'leaderboard:update';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Update the Leaderboard and dispatch the rewards for the top 3 users.';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        // Fetch the first 3 users that have the most experience and NOT the role banished.
43
        $users = User::whereDoesntHave('roles', function (Builder $query) {
44
            $query->where('slug', 'banished');
45
        })
46
        ->orderBy('experiences_total', 'desc')
47
        ->limit(3)
48
        ->get();
49
50
        foreach ($users as $user) {
51
            //Dispatch the event.
52
            event(new LeaderboardEvent($user));
53
        }
54
    }
55
}
56