Cacher::getCacheContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
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