CacheNormalizer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 15
c 4
b 1
f 0
dl 0
loc 38
ccs 15
cts 17
cp 0.8824
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 24 8
A makeFromSeconds() 0 3 1
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class CacheNormalizer implements NormalizerContract
8
{
9
    /**
10
     * ّFigures out how long the cache life time should be.
11
     *
12
     * @param  object  $widget
13
     * @return void
14
     */
15 24
    public function normalize($widget): void
16
    {
17 24
        if (! property_exists($widget, 'cacheLifeTime')) {
18 21
            $M = config('widgetize.default_cache_lifetime', 0);
19 21
            $widget->cacheLifeTime = $this->makeFromSeconds($M * 60);
20
        }
21
22 24
        if (! property_exists($widget, 'cacheView')) {
23 23
            $widget->cacheView = true;
24
        }
25
26 24
        if ($widget->cacheLifeTime === 0) {
27 1
            $widget->cacheLifeTime = $this->makeFromSeconds(0);
28
        }
29
30 24
        if (is_object($widget->cacheLifeTime)) {
31 22
            return;
32
        }
33
34 2
        if ($widget->cacheLifeTime === 'forever' || $widget->cacheLifeTime < 0) {
35
            // 1209600 seconds is 2 weeks, which is long enough !
36 2
            $widget->cacheLifeTime = $this->makeFromSeconds(1209600);
37
        } elseif (is_numeric($widget->cacheLifeTime)) {
38
            $widget->cacheLifeTime = $this->makeFromSeconds($widget->cacheLifeTime * 60);
39
        }
40 2
    }
41
42 24
    public function makeFromSeconds($s)
43
    {
44 24
        return new \DateInterval('PT'.(string) ceil($s).'S');
45
    }
46
}
47