Completed
Push — master ( fbc7c8...750795 )
by Iman
02:06
created

CacheNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeCacheLifeTime() 0 10 3
B normalizeCacheTags() 0 16 5
A cacheCanUseTags() 0 4 1
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
class CacheNormalizer
6
{
7
    /**
8
     * ّFigures out how long the cache life time should be.
9
     * @param $widget
10
     * @return null
11
     */
12
    public function normalizeCacheLifeTime($widget)
13
    {
14
        if ($widget->cacheLifeTime === 'env_default') {
15
            $widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0));
16
        }
17
18
        if ($widget->cacheLifeTime === 'forever') {
19
            $widget->cacheLifeTime = -1;
20
        }
21
    }
22
23
    /**
24
     * ّFigures out what the cache tags should be.
25
     * @param $widget
26
     * @return null
27
     */
28
    public function normalizeCacheTags($widget)
29
    {
30
        if (!$this->cacheCanUseTags() || !$widget->cacheTags) {
31
            return $widget->cacheTags = null;
32
        }
33
34
        if (is_array($widget->cacheTags)) {
35
            return $widget->cacheTags;
36
        }
37
38
        if (is_string($widget->cacheTags)) {
39
            return $widget->cacheTags = [$widget->cacheTags];
40
        }
41
42
        throw new \InvalidArgumentException('Cache Tags should be of type String or Array.');
43
    }
44
45
    /**
46
     * Determine whether cache tags should be applied or not
47
     * @return bool
48
     */
49
    private function cacheCanUseTags()
50
    {
51
        return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']);
52
    }
53
}
54