Skip to content

Commit cba71b8

Browse files
committed
fixed time frames
1 parent da1026b commit cba71b8

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

app/Http/Controllers/NetifyLicenseController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Logic\NetifydLicenseRepository;
66
use App\NetifydLicenseType;
7+
use Carbon\Carbon;
78
use Exception;
89
use Illuminate\Http\JsonResponse;
910
use Illuminate\Support\Facades\Cache;
@@ -44,22 +45,20 @@ private function run(NetifydLicenseRepository $licenseProvider, NetifydLicenseTy
4445
}
4546
// Got license, checking if everything is in place.
4647
Log::debug('Netifyd license recovered from remote server, checking if it can be renewed.');
47-
$expiration = $license['expire_at']['unix'];
48-
$creation = $license['created_at']['unix'];
49-
$renewalThreshold = ($expiration - $creation) / 2 + $creation;
50-
$now = now()->unix();
51-
if ($renewalThreshold < $now) {
48+
$expiration = Carbon::createFromTimestampUTC($license['expire_at']['unix'])->startOfDay()->toImmutable();
49+
$creation = Carbon::createFromTimestampUTC($license['created_at']['unix'])->startOfDay()->toImmutable();
50+
$diff = $creation->diff($expiration)->cascade()->totalDays;
51+
$renewalThreshold = $creation->addDays(ceil($diff / 2));
52+
$now = now()->utc()->startOfDay();
53+
if ($renewalThreshold <= $now) {
5254
Log::debug('Netifyd license can be renewed, renewing it.');
5355
try {
5456
$license = $licenseProvider->renewLicense($licenseType, $license['serial']);
5557
} catch (Exception $e) {
5658
return response()->json(['message' => $e->getMessage()], 500);
5759
}
5860
}
59-
60-
$expiration = $license['expire_at']['unix'];
61-
$creation = $license['created_at']['unix'];
62-
Cache::put($licenseType->cacheLabel(), $license, ($expiration - $creation) / 2);
61+
Cache::put($licenseType->cacheLabel(), $license, now()->addHour());
6362

6463
return response()->json($license);
6564
}

0 commit comments

Comments
 (0)