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

Cache::cacheResult()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 11
nc 8
nop 3
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
}