GrabStat   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 18
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A insertTopGrabbers() 0 6 2
A getTopGrabbers() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
8
class GrabStat extends Model
9
{
10
    use HasFactory;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Database\Eloquent\Factories\HasFactory requires the property $factoryClass which is not provided by App\Models\GrabStat.
Loading history...
11
12
    protected $guarded = [];
13
14
    public static function insertTopGrabbers(): void
15
    {
16
        $users = User::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc('grabs')->limit(10)->get();
0 ignored issues
show
Bug introduced by
'grabs' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
        $users = User::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc(/** @scrutinizer ignore-type */ 'grabs')->limit(10)->get();
Loading history...
17
        // Insert data into the grab_stats table
18
        foreach ($users as $user) {
19
            self::updateOrCreate(['username' => $user->username], ['grabs' => $user->grabs]);
20
        }
21
    }
22
23
    public static function getTopGrabbers(): array
24
    {
25
        return self::query()->select(['username', 'grabs'])->get()->toArray();
26
    }
27
}
28