SignupStat   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 19
c 0
b 0
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsersByMonth() 0 7 1
A insertUsersByMonth() 0 14 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
8
class SignupStat 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\SignupStat.
Loading history...
11
12
    protected $guarded = [];
13
14
    public static function insertUsersByMonth(): void
15
    {
16
        $users = User::query()
17
            ->whereNotNull('created_at')
18
            ->where('created_at', '<>', '0000-00-00 00:00:00')
19
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-01') as sort_date, DATE_FORMAT(created_at, '%M %Y') as mth, COUNT(id) as num")
20
            ->groupBy(['sort_date', 'mth'])
21
            ->orderByDesc('sort_date')
0 ignored issues
show
Bug introduced by
'sort_date' 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

21
            ->orderByDesc(/** @scrutinizer ignore-type */ 'sort_date')
Loading history...
22
            ->get();
23
24
        foreach ($users as $user) {
25
            self::updateOrCreate(
26
                ['month' => $user->mth],
27
                ['signups' => $user->num, 'sort_date' => $user->sort_date]
28
            );
29
        }
30
    }
31
32
    public static function getUsersByMonth(): array
33
    {
34
        return self::query()
35
            ->select(['month', 'signups'])
36
            ->orderByDesc('sort_date')
0 ignored issues
show
Bug introduced by
'sort_date' 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

36
            ->orderByDesc(/** @scrutinizer ignore-type */ 'sort_date')
Loading history...
37
            ->get()
38
            ->toArray();
39
    }
40
}
41