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()
18
            ->with('parent')
19
            ->where('r.adddate', '>', now()->subWeek())
20
            ->select([
21
                'categories.id',
22
                'root_categories_id',
23
                DB::raw('COUNT(r.id) as count'),
24
                'categories.title',
25
            ])
26
            ->join('releases as r', 'r.categories_id', '=', 'categories.id')
27
            ->whereNotIn('categories.id', [10, 20]) // Exclude OTHER_MISC and OTHER_HASHED
28
            ->groupBy('categories.id', 'root_categories_id', 'categories.title')
29
            ->orderByDesc('count')
30
            ->get();
31
32
        foreach ($categories as $category) {
33
            // Build the category display name with root category prefix
34
            $categoryDisplay = $category->parent
35
                ? $category->parent->title.' > '.$category->title
36
                : $category->title;
37
38
            // Check if we already have the information and if we do just update the count
39
            if (self::query()->where('category', $categoryDisplay)->exists()) {
40
                self::query()->where('category', $categoryDisplay)->update(['count' => $category->count]);
41
42
                continue;
43
            }
44
            self::query()->create(['category' => $categoryDisplay, 'count' => $category->count]);
45
        }
46
    }
47
48
    public static function getRecentlyAdded(): array
49
    {
50
        return self::query()->select(['category', 'count'])->get()->toArray();
51
    }
52
}
53