Cache::makeCacheKey()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 19
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9666
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils;
4
5
class Cache
6
{
7
    /**
8
     * @var \Imanghafoori\Widgets\Utils\CacheTag
9
     */
10
    private $_cacheTag;
11
12
    /**
13
     * Cache constructor.
14
     */
15 22
    public function __construct()
16
    {
17 22
        $this->_cacheTag = app(CacheTag::class);
18 22
    }
19
20
    /**
21
     * Caches the widget output.
22
     *
23
     * @param  array  $args
24
     * @param  callable  $phpCode
25
     * @param  object  $widgetObj
26
     * @param  string  $form
27
     * @return null
28
     */
29 22
    public function cacheResult(array $args, callable $phpCode, $widgetObj, $form = 'HTML')
30
    {
31 22
        if (! resolve(Policies::class)->widgetShouldUseCache() || $widgetObj->cacheLifeTime->s === 0) {
32 16
            return $phpCode();
33
        }
34
35 6
        $cache = resolve('cache');
36
37 6
        if (! empty($widgetObj->cacheTags) && $this->cacheDriverSupportsTags()) {
38
            $cache = $cache->tags($widgetObj->cacheTags);
39
        }
40
41 6
        $key = $this->makeCacheKey($args, $widgetObj, $form);
42
43 6
        return $cache->remember($key, $widgetObj->cacheLifeTime, $phpCode);
44
    }
45
46
    /**
47
     * Creates a unique cache key for each possible output.
48
     *
49
     * @param  array  $arg
50
     * @param  object  $widget
51
     * @param  string  $form
52
     * @return string An MD5 string
53
     */
54 6
    private function makeCacheKey(array $arg, $widget, string $form): string
55
    {
56 6
        if (method_exists($widget, 'cacheKey')) {
57 1
            return $widget->cacheKey($arg);
58
        }
59
60 5
        $_key = '';
61
62 5
        if (method_exists($widget, 'extraCacheKeyDependency')) {
63
            $_key = json_encode($widget->extraCacheKeyDependency($arg));
64
        }
65
66 5
        if (! $this->cacheDriverSupportsTags()) {
67 1
            $_key .= json_encode($this->getTagTokens($widget->cacheTags));
68
        }
69
70 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

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