ReleaseStat::getRecentlyAdded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
9
class ReleaseStat extends Model
10
{
11
    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\ReleaseStat.
Loading history...
12
13
    protected $guarded = [];
14
15
    public static function insertRecentlyAdded(): void
16
    {
17
        $categories = Category::query()->with('parent')->where('r.adddate', '>', now()->subWeek())->select([
18
            'root_categories_id', DB::raw('COUNT(r.id) as count'), 'title',
19
        ])->join('releases as r', 'r.categories_id', '=',
20
            'categories.id')->groupBy('title')->orderByDesc('count')->get();
0 ignored issues
show
Bug introduced by
'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

20
            'categories.id')->groupBy('title')->orderByDesc(/** @scrutinizer ignore-type */ 'count')->get();
Loading history...
21
22
        foreach ($categories as $category) {
23
            // Check if we already have the information and if we do just update the count
24
            if (self::query()->where('category', $category->title)->exists()) {
25
                self::query()->where('category', $category->title)->update(['count' => $category->count]);
26
27
                continue;
28
            }
29
            self::query()->create(['category' => $category->title, 'count' => $category->count]);
30
        }
31
    }
32
33
    public static function getRecentlyAdded(): array
34
    {
35
        return self::query()->select(['category', 'count'])->get()->toArray();
36
    }
37
}
38