Passed
Push — master ( 67253e...1a1f73 )
by Arthur
04:53 queued 11s
created

ModelCache::findWithoutRequery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 11.10.18
6
 * Time: 14:11
7
 */
8
9
namespace Foundation\Cache;
10
11
12
use Cache;
13
use Foundation\Exceptions\Exception;
14
use Illuminate\Support\Facades\Redis;
15
16
class ModelCache
17
{
18
19
    /**
20
     * @param $id
21
     * @param string $modelClass
22
     * @return mixed
23
     */
24 3
    public static function find($id, $modelClass)
25
    {
26
        return Cache::remember(self::getCacheName($id, $modelClass), self::getCacheTime(), function () use ($id, $modelClass) {
27
            return $modelClass::find($id);
28 3
        });
29
    }
30
31
    /**
32
     * @param string
33
     */
34 17
    public static function getCacheName($id, $modelClass)
35
    {
36 17
        return config('model.cache_prefix') . ':' . strtolower(getShortClassName($modelClass)) . ':' . $id;
37
    }
38
39
    /**
40
     * @return \Illuminate\Config\Repository|mixed
41
     */
42 17
    private static function getCacheTime()
43
    {
44 17
        return config('model.cache_time');
45
    }
46
47
    /**
48
     * @param $id
49
     * @param string $modelClass
50
     * @return mixed
51
     */
52 2
    public static function findWithoutRequery($id, $modelClass)
53
    {
54 2
        return Cache::get(self::getCacheName($id, $modelClass));
55
    }
56
57
    /**
58
     * @param \Eloquent $model
59
     */
60 17
    public static function store($model)
61
    {
62 17
        Cache::put(self::getCacheName($model->getKey(), $model), $model->refresh(), self::getCacheTime());
63 17
    }
64
65
    /**
66
     * @param $id
67
     * @param string $modelClass
68
     * @return bool
69
     */
70
    public static function remove($id, $modelClass)
71
    {
72
        return Cache::forget(self::getCacheName($id, $modelClass));
73
    }
74
75
    /**
76
     *
77
     */
78 1
    public static function clearAll()
79
    {
80 1
        $pattern = config('model.cache_prefix');
81 1
        self::deleteWithPrefix($pattern);
82 1
    }
83
84
    /**
85
     * @param $prefix
86
     * @throws Exception
87
     */
88 2
    private static function deleteWithPrefix($prefix)
89
    {
90 2
        $redis = self::getCacheConnection();
91 2
        $keyPattern = Cache::getPrefix() . $prefix . '*';
0 ignored issues
show
Bug introduced by
The call to Illuminate\Cache\CacheManager::getPrefix() has too few arguments starting with config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $keyPattern = Cache::/** @scrutinizer ignore-call */ getPrefix() . $prefix . '*';

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
92 2
        $keys = $redis->keys($keyPattern);
93 2
        $redis->delete($keys);
94 2
    }
95
96
97
    /**
98
     * @return \Illuminate\Redis\Connections\Connection
99
     * @throws Exception
100
     */
101 2
    private static function getCacheConnection()
102
    {
103 2
        if (config('cache.default') === 'redis')
104 2
            return Redis::connection('cache');
105
106
        throw new Exception("This action is only possible with redis as cache driver");
107
    }
108
109
    /**
110
     * @param $modelClass
111
     * @throws Exception
112
     */
113 1
    public static function clearModel($modelClass)
114
    {
115 1
        $pattern = config('model.cache_prefix') . ':' . strtolower(getShortClassName($modelClass));
116 1
        self::deleteWithPrefix($pattern);
117 1
    }
118
119
}
120