Passed
Push — master ( f0334d...12f38b )
by Moecasts
07:32
created

Cacher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
ccs 18
cts 18
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheContent() 0 11 2
A hasCache() 0 3 1
A pullCache() 0 3 1
A setCache() 0 8 1
A getCacheKey() 0 3 1
1
<?php
2
3
namespace Moecasts\Laravel\UserLoginLog;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Cache;
7
8
class Cacher
9
{
10 7
    public static function getCacheKey($loggable)
11
    {
12 7
        return $loggable->getMorphClass() . '-' . $loggable->getKey() . '-login-log';
13
    }
14
15 7
    public static function getCacheContent($loggable)
16
    {
17 7
        if (!empty($cache = Cache::get(static::getCacheKey($loggable)))) {
18 2
            return $cache;
19
        }
20
21 7
        $cachedAt = Carbon::now();
22
23
        return [
24 7
            'cachedAt' => $cachedAt,
25 7
            'loggable' => $loggable,
26
        ];
27
    }
28
29 7
    public static function hasCache($loggable)
30
    {
31 7
        return Cache::has(static::getCacheKey($loggable));
32
    }
33
34 7
    public static function setCache($loggable, $seconds = null)
35
    {
36 7
        $seconds = $seconds ?? config('loginlog.expire', 60);
37
38 7
        return Cache::put(
39 7
            static::getCacheKey($loggable),
40 7
            static::getCacheContent($loggable),
41 7
            now()->addSeconds($seconds)
42
        );
43
    }
44
45 1
    public static function pullCache($loggable)
46
    {
47 1
        return Cache::pull(static::getCacheKey($loggable));
48
    }
49
}
50