CooldownServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 3 1
A cleanUpExpiredCooldowns() 0 15 5
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
It seems like you are loosely comparing $hoursAgo of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
42
            try {
43
                Cooldown::expired()->delete();
44
            }
45
            catch(Exception $e) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
46
47
            Cache::forever('kurozora_cooldown_last_cleaned_up_at', now());
48
        }
49
    }
50
}