Passed
Push — master ( ab23d3...456c2a )
by Ferry
05:06
created

CacheHelper::putInGroup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 8/16/2019
6
 * Time: 1:29 AM
7
 */
8
9
namespace crocodicstudio\crudbooster\helpers;
10
11
12
use Illuminate\Support\Facades\Cache;
13
14
class CacheHelper
15
{
16
    public static function putInGroup($key, $value, $group, $duration) {
17
        $key = md5($key);
18
        $exist = (Cache::get($group))?:[];
19
        $exist[ $key ] = ['value'=>$value,'duration'=>$duration];
20
21
        if($duration == -1) {
22
            Cache::forever($group, $exist);
23
        } else {
24
            Cache::put($group, $exist, $duration);
25
        }
26
    }
27
28
    public static function getItemInGroup($key, $group) {
29
        $groupdata = Cache::get($group);
30
        $key = md5($key);
31
        if(isset($groupdata[$key])) {
32
            return $groupdata[$key]['value'];
33
        }
34
        return null;
35
    }
36
37
    public static function forgetGroup($group) {
38
        Cache::forget($group);
39
    }
40
41
    public static function forgetInGroup($key,$group) {
42
        $key = md5($key);
43
        $data = Cache::get($group);
44
        $duration = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $duration is dead and can be removed.
Loading history...
45
        if(isset($data[$key])) {
46
            $item = $data[$key];
47
            unset($data[$key]);
48
        }
49
        Cache::put($group, $exist, $item['duration']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $exist seems to be never defined.
Loading history...
50
        return true;
51
    }
52
53
}