Passed
Push — master ( 4f2421...1f8a01 )
by Darko
10:00
created

UserDownload::getDownloadRequestsForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
8
/**
9
 * App\Models\UserDownload.
10
 *
11
 * @property int $id
12
 * @property int $users_id
13
 * @property string $hosthash
14
 * @property string $timestamp
15
 * @property int $releases_id FK to releases.id
16
 * @property-read Release $release
17
 * @property-read User $user
18
 *
19
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereHosthash($value)
20
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereId($value)
21
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereReleasesId($value)
22
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereTimestamp($value)
23
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload whereUsersId($value)
24
 *
25
 * @mixin \Eloquent
26
 *
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload newModelQuery()
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload newQuery()
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDownload query()
30
 */
31
class UserDownload extends Model
32
{
33
    /**
34
     * @var bool
35
     */
36
    protected $dateFormat = false;
37
38
    /**
39
     * @var bool
40
     */
41
    public $timestamps = false;
42
43
    /**
44
     * @var array
45
     */
46
    protected $guarded = [];
47
48
    public function user(): BelongsTo
49
    {
50
        return $this->belongsTo(User::class, 'users_id');
51
    }
52
53
    public function release(): BelongsTo
54
    {
55
        return $this->belongsTo(Release::class, 'releases_id');
56
    }
57
58
    /**
59
     * Get the COUNT of how many NZB's the user has downloaded in the past day.
60
     *
61
     *
62
     * @throws \Exception
63
     */
64
    public static function getDownloadRequests(int $userID): int
65
    {
66
        // Clear old requests.
67
        self::whereUsersId($userID)->where('timestamp', '<', now()->subDay())->delete();
68
        $value = self::whereUsersId($userID)->where('timestamp', '>', now()->subDay())->count('id');
69
70
        return $value === false ? 0 : $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value === false ? 0 : $value could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
71
    }
72
73
    /**
74
     * Get hourly download counts for the last 24 hours.
75
     *
76
     * @return array Array of hourly counts indexed by hour
77
     *
78
     * @throws \Exception
79
     */
80
    public static function getHourlyDownloads(int $userID): array
81
    {
82
        $hourlyData = [];
83
        $now = now();
84
85
        // Initialize all 24 hours with 0
86
        for ($i = 23; $i >= 0; $i--) {
87
            $hour = $now->copy()->subHours($i);
88
            $hourlyData[$hour->format('H:00')] = 0;
89
        }
90
91
        // Get downloads from the last 24 hours grouped by hour
92
        $downloads = self::whereUsersId($userID)
93
            ->where('timestamp', '>', $now->subDay())
94
            ->get();
95
96
        foreach ($downloads as $download) {
97
            $hourKey = \Carbon\Carbon::parse($download->timestamp)->format('H:00');
98
            if (isset($hourlyData[$hourKey])) {
99
                $hourlyData[$hourKey]++;
100
            }
101
        }
102
103
        return $hourlyData;
104
    }
105
106
    /**
107
     * @return \Illuminate\Database\Eloquent\Collection|static[]
108
     */
109
    public static function getDownloadRequestsForUser($userID)
110
    {
111
        return self::whereUsersId($userID)->with('release')->orderByDesc('timestamp')->get();
112
    }
113
114
    /**
115
     * If a user downloads a NZB, log it.
116
     *
117
     *
118
     * @return int|\Illuminate\Database\Eloquent\Builder
119
     */
120
    public static function addDownloadRequest($userID, $releaseID)
121
    {
122
        return self::query()
123
            ->insertGetId(
124
                [
125
                    'users_id' => $userID,
126
                    'releases_id' => $releaseID,
127
                    'timestamp' => now(),
128
                ]
129
            );
130
    }
131
132
    /**
133
     * @return mixed
134
     *
135
     * @throws \Exception
136
     */
137
    public static function delDownloadRequestsForRelease(int $releaseID)
138
    {
139
        return self::whereReleasesId($releaseID)->delete();
140
    }
141
142
    /**
143
     * @throws \Exception
144
     */
145
    public static function delDownloadRequests($userID): void
146
    {
147
        self::whereUsersId($userID)->delete();
148
    }
149
}
150