Completed
Push — master ( eb1ea8...814c2d )
by Iman
03:08
created

Cache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B cacheResult() 0 23 5
A _makeCacheKey() 0 4 1
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
class Cache
6
{
7
    /**
8
     * Caches the widget output
9
     *
10
     * @param $args
11
     * @param $phpCode
12
     * @param $widget
13
     *
14
     * @return null
15
     */
16
    public function cacheResult($args, $phpCode, $widget)
17
    {
18
        $key = $this->_makeCacheKey($args, $widget);
19
20
        $cache = app('cache');
21
22
        if (is_array($widget->cacheTags)) {
23
            $cache = $cache->tags($widget->cacheTags);
24
        }
25
26
        if ($widget->cacheLifeTime > 0) {
27
            return $cache->remember($key, $widget->cacheLifeTime, $phpCode);
28
        }
29
30
        if ($widget->cacheLifeTime < 0) {
31
            return $cache->rememberForever($key, $phpCode);
32
        }
33
34
        if ($widget->cacheLifeTime === 0) {
35
            return $phpCode();
36
        }
37
        return null;
38
    }
39
40
    /**
41
     * Creates a unique cache key for each possible output
42
     *
43
     * @param $arg
44
     * @param $widget
45
     *
46
     * @return string
47
     */
48
    private function _makeCacheKey($arg, $widget)
49
    {
50
        return md5(json_encode($arg, JSON_FORCE_OBJECT) . $widget->template . class_basename($widget));
51
    }
52
}
53