Passed
Push — master ( a23672...3dee81 )
by Iman
08:02
created

CacheNormalizer::makeFromSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 16
    public function normalize($widget): void
16
    {
17 16
        if (! property_exists($widget, 'cacheLifeTime')) {
18 13
            $M = config('widgetize.default_cache_lifetime', 0);
19 13
            $widget->cacheLifeTime = $this->makeFromSeconds($M* 60);
20
        }
21
22 16
        if($widget->cacheLifeTime === 0) {
23 1
            $widget->cacheLifeTime = $this->makeFromSeconds(0);
24
        }
25
26 16
        if (is_object($widget->cacheLifeTime)) {
27 14
            return;
28
        }
29
30 2
        if ($widget->cacheLifeTime === 'forever' || $widget->cacheLifeTime < 0) {
31
            // 2 weeks which is long enough !
32 2
            $widget->cacheLifeTime = $this->makeFromSeconds(1209600);
33
        } elseif (is_numeric($widget->cacheLifeTime)) {
34
            $widget->cacheLifeTime = $this->makeFromSeconds($widget->cacheLifeTime * 60);
35
        }
36 2
    }
37
38 16
    public function makeFromSeconds($s)
39
    {
40 16
        return new \DateInterval('PT'.(string)ceil($s).'S');
41
    }
42
}
43