Passed
Push — master ( 48c78c...6f5874 )
by Darko
06:40
created

RoleStat   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 17
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsersByRole() 0 3 1
A insertUsersByRole() 0 5 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\Models\Role;
8
9
class RoleStat extends Model
10
{
11
    use HasFactory;
12
13
    protected $guarded = [];
14
15
    public static function insertUsersByRole(): void
16
    {
17
        $roles = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get();
0 ignored issues
show
Bug introduced by
'users_count' 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

17
        $roles = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc(/** @scrutinizer ignore-type */ 'users_count')->get();
Loading history...
18
        foreach ($roles as $role) {
19
            self::updateOrCreate(['role' => $role->name, 'users' => $role->users_count]);
20
        }
21
    }
22
23
    public static function getUsersByRole(): array
24
    {
25
        return self::query()->select(['role', 'users'])->get()->toArray();
26
    }
27
}
28