Passed
Push — master ( 2f2795...71165f )
by Iman
06:01
created

Cache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 103
ccs 29
cts 33
cp 0.8788
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTagTokens() 0 5 1
A expireTaggedWidgets() 0 12 4
A makeCacheKey() 0 19 4
A cacheResult() 0 15 4
A __construct() 0 3 1
A cacheDriverSupportsTags() 0 3 1
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
class Cache
6
{
7
    private $_cacheTag;
8
9
    /**
10
     * Cache constructor.
11
     */
12 6
    public function __construct()
13
    {
14 6
        $this->_cacheTag = app(CacheTag::class);
15 6
    }
16
17
    /**
18
     * Caches the widget output.
19
     *
20
     * @param array $args
21
     * @param callable $phpCode
22
     * @param object $widgetObj
23
     *
24
     * @param string $form
25
     * @return null
26
     */
27 6
    public function cacheResult(array $args, callable $phpCode, $widgetObj, $form = 'HTML')
28
    {
29 6
        $key = $this->makeCacheKey($args, $widgetObj, $form);
30
31 6
        $cache = app('cache');
32
33 6
        if (! empty($widgetObj->cacheTags) && $this->cacheDriverSupportsTags()) {
34
            $cache = $cache->tags($widgetObj->cacheTags);
35
        }
36
37 6
        if ($widgetObj->cacheLifeTime === 0) {
38 1
            return $phpCode();
39
        }
40
41 5
        return $cache->remember($key, $widgetObj->cacheLifeTime, $phpCode);
42
    }
43
44
    /**
45
     * Creates a unique cache key for each possible output.
46
     *
47
     * @param array $arg
48
     * @param object $widget
49
     * @param string $form
50
     * @return string An MD5 string
51
     */
52 6
    private function makeCacheKey(array $arg, $widget, $form)
53
    {
54 6
        if (method_exists($widget, 'cacheKey')) {
55 1
            return $widget->cacheKey($arg);
56
        }
57
58 5
        $_key = '';
59
60 5
        if (method_exists($widget, 'extraCacheKeyDependency')) {
61
            $_key = json_encode($widget->extraCacheKeyDependency($arg));
62
        }
63
64 5
        if (! $this->cacheDriverSupportsTags()) {
65 1
            $_key .= json_encode($this->getTagTokens($widget->cacheTags));
66
        }
67
68 5
        $_key .= json_encode($arg, JSON_FORCE_OBJECT).app()->getLocale().$form.$widget->template.get_class($widget);
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

68
        $_key .= json_encode($arg, JSON_FORCE_OBJECT).app()->/** @scrutinizer ignore-call */ getLocale().$form.$widget->template.get_class($widget);
Loading history...
69
70 5
        return md5($_key);
71
    }
72
73
    /**
74
     * Determines cacheTagging is supported by the chosen laravel cache driver or not.
75
     * @return bool
76
     */
77 5
    private function cacheDriverSupportsTags()
78
    {
79 5
        return ! in_array(config('cache.default', 'file'), ['file', 'database']);
80
    }
81
82
    /**
83
     * @param string[] $cacheTags
84
     * @return string[]
85
     */
86
    private function getTagTokens(array $cacheTags)
87
    {
88 1
        return array_map(function ($tag) {
89 1
            return $this->_cacheTag->getTagToken($tag);
90 1
        }, $cacheTags);
91
    }
92
93
    /**
94
     * @param string[] $tags
95
     */
96 1
    public function expireTaggedWidgets($tags)
97
    {
98 1
        if ($this->cacheDriverSupportsTags()) {
99
            return \Cache::tags($tags)->flush();
100
        }
101
102 1
        if (is_string($tags)) {
0 ignored issues
show
introduced by
The condition is_string($tags) is always false.
Loading history...
103
            $tags = [$tags];
104
        }
105
106 1
        foreach ($tags as $tag) {
107 1
            $this->_cacheTag->generateNewToken($tag);
108
        }
109 1
    }
110
}
111