CacheNormalizer::normalize()   B
last analyzed

Complexity

Conditions 8
Paths 32

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.1867

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 13
c 2
b 1
f 0
nc 32
nop 1
dl 0
loc 24
ccs 12
cts 14
cp 0.8571
crap 8.1867
rs 8.4444
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