Passed
Branch support-laravel-6 (ddd0b3)
by Pieter
06:42
created

LifetimeHelper::isLegacy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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