Completed
Pull Request — master (#16)
by Graham
05:22
created

LifetimeHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A computeLifetime() 0 8 2
A isLegacy() 0 11 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