|
4 | 4 |
|
5 | 5 | use App\Logic\NetifydLicenseRepository; |
6 | 6 | use App\NetifydLicenseType; |
| 7 | +use Carbon\Carbon; |
7 | 8 | use Exception; |
8 | 9 | use Illuminate\Http\JsonResponse; |
9 | 10 | use Illuminate\Support\Facades\Cache; |
@@ -44,22 +45,20 @@ private function run(NetifydLicenseRepository $licenseProvider, NetifydLicenseTy |
44 | 45 | } |
45 | 46 | // Got license, checking if everything is in place. |
46 | 47 | 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) { |
52 | 54 | Log::debug('Netifyd license can be renewed, renewing it.'); |
53 | 55 | try { |
54 | 56 | $license = $licenseProvider->renewLicense($licenseType, $license['serial']); |
55 | 57 | } catch (Exception $e) { |
56 | 58 | return response()->json(['message' => $e->getMessage()], 500); |
57 | 59 | } |
58 | 60 | } |
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()); |
63 | 62 |
|
64 | 63 | return response()->json($license); |
65 | 64 | } |
|
0 commit comments