LifetimeHelper::isLegacy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
namespace Madewithlove\IlluminatePsrCacheBridge\Laravel;
3
4
use DateTimeImmutable;
5
use DateTimeInterface;
6
use Exception;
7
use Illuminate\Contracts\Cache\Store;
8
use ReflectionClass;
9
10
class LifetimeHelper
11
{
12 5
    public static function computeLifetime(DateTimeInterface $expiresAt)
13
    {
14 5
        $now = new DateTimeImmutable('now', $expiresAt->getTimezone());
15
16 5
        $seconds = $expiresAt->getTimestamp() - $now->getTimestamp();
17
18 5
        return self::isLegacy() ? (int) floor($seconds / 60.0) : $seconds;
19
    }
20
21 5
    private static function isLegacy()
22
    {
23 5
        static $legacy;
24
25 5
        if ($legacy === null) {
26 1
            $params = (new ReflectionClass(Store::class))->getMethod('put')->getParameters();
27 1
            $legacy = $params[2]->getName() === 'minutes';
28
        }
29
30 5
        return $legacy;
31
    }
32
}
33