Completed
Push — master ( 14a525...bc69d0 )
by Iman
02:10
created

Cache::makeCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
6
class Cache
7
{
8
9
    /**
10
     * @param $arg
11
     * @param $widget
12
     * @return string
13
     */
14
    private function makeCacheKey($arg, $widget)
15
    {
16
        return md5(json_encode($arg, JSON_FORCE_OBJECT) . $widget->template . class_basename($widget));
17
    }
18
19
    function cacheResult($args, $phpCode, $widget)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        $key = $this->makeCacheKey($args,$widget);
22
23
        $cache = app('cache');
24
25
        if ($widget->cacheTags) {
26
            $cache = $cache->tags($widget->cacheTags);
27
        }
28
29
        if ($widget->cacheLifeTime > 0) {
30
            return $cache->remember($key, $widget->cacheLifeTime, $phpCode);
31
        }
32
33
        if ($widget->cacheLifeTime < 0) {
34
            return $cache->rememberForever($key, $phpCode);
35
        }
36
37
        if ($widget->cacheLifeTime === 0) {
38
            return $phpCode();
39
        }
40
    }
41
42
}