1 | <?php |
||
2 | |||
3 | namespace Kurozora\Cooldown; |
||
4 | |||
5 | use Illuminate\Support\Carbon; |
||
6 | use Illuminate\Support\Facades\Cache; |
||
7 | use Illuminate\Support\ServiceProvider; |
||
8 | use Kurozora\Cooldown\Models\Cooldown; |
||
9 | use \Exception; |
||
10 | |||
11 | class CooldownServiceProvider extends ServiceProvider |
||
12 | { |
||
13 | /** |
||
14 | * Bootstrap the application services. |
||
15 | */ |
||
16 | public function boot() |
||
17 | { |
||
18 | $this->loadMigrationsFrom(__DIR__ . '/Migrations'); |
||
19 | $this->cleanUpExpiredCooldowns(); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Register the application services. |
||
24 | */ |
||
25 | public function register() |
||
26 | { |
||
27 | require 'helpers.php'; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Deletes all expired cooldowns. Only runs once per day. |
||
32 | */ |
||
33 | public function cleanUpExpiredCooldowns() |
||
34 | { |
||
35 | // Get the datetime at which the cooldowns were last cleaned up |
||
36 | /** @var Carbon $lastDeletedAt */ |
||
37 | $lastDeletedAt = Cache::get('kurozora_cooldown_last_cleaned_up_at', null); |
||
38 | $hoursAgo = $lastDeletedAt == null ? null : $lastDeletedAt->diffInHours(now()); |
||
39 | |||
40 | // Delete the cooldowns if the last run was 24 hours ago or more |
||
41 | if($hoursAgo >= 24 || $hoursAgo == null) { |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
42 | try { |
||
43 | Cooldown::expired()->delete(); |
||
44 | } |
||
45 | catch(Exception $e) { } |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
46 | |||
47 | Cache::forever('kurozora_cooldown_last_cleaned_up_at', now()); |
||
48 | } |
||
49 | } |
||
50 | } |