Passed
Push — master ( 9fec5e...8bb2fe )
by Iman
02:14
created

Cache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 100
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTagTokens() 0 5 1
A makeCacheKey() 0 19 4
A cacheResult() 0 15 4
A __construct() 0 3 1
A cacheDriverSupportsTags() 0 3 1
A expireTaggedWidgets() 0 8 3
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, string $form): string
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() : bool
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[]|string $tags
95
     * @return void
96
     */
97 1
    public function expireTaggedWidgets($tags)
98
    {
99 1
        if ($this->cacheDriverSupportsTags()) {
100
            return \Cache::tags($tags)->flush();
101
        }
102
103 1
        foreach ((array) $tags as $tag) {
104 1
            $this->_cacheTag->generateNewToken($tag);
105
        }
106 1
    }
107
}
108