|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers; |
4 | 4 |
|
5 | | -use App\Logic\NetifydLicenceRepository; |
6 | | -use App\NetifydLicenceType; |
| 5 | +use App\Logic\NetifydLicenseRepository; |
| 6 | +use App\NetifydLicenseType; |
7 | 7 | use Exception; |
8 | 8 | use Illuminate\Http\JsonResponse; |
9 | 9 | use Illuminate\Support\Facades\Cache; |
10 | 10 | use Illuminate\Support\Facades\Log; |
11 | 11 |
|
12 | | -class NetifyLicenceController extends Controller |
| 12 | +class NetifyLicenseController extends Controller |
13 | 13 | { |
14 | | - public function community(NetifydLicenceRepository $licenceProvider): JsonResponse |
| 14 | + public function community(NetifydLicenseRepository $licenseProvider): JsonResponse |
15 | 15 | { |
16 | | - return $this->run($licenceProvider, NetifydLicenceType::COMMUNITY); |
| 16 | + return $this->run($licenseProvider, NetifydLicenseType::COMMUNITY); |
17 | 17 | } |
18 | 18 |
|
19 | | - private function run(NetifydLicenceRepository $licenceProvider, NetifydLicenceType $licenceType): JsonResponse |
| 19 | + private function run(NetifydLicenseRepository $licenseProvider, NetifydLicenseType $licenseType): JsonResponse |
20 | 20 | { |
21 | 21 | // If license is in cache, return it. |
22 | | - if (Cache::has($licenceType->cacheLabel())) { |
23 | | - Log::debug('Requested netifyd licence found in cache, returning it.'); |
| 22 | + if (Cache::has($licenseType->cacheLabel())) { |
| 23 | + Log::debug('Requested netifyd license found in cache, returning it.'); |
24 | 24 |
|
25 | | - return response()->json(Cache::get($licenceType->cacheLabel())); |
| 25 | + return response()->json(Cache::get($licenseType->cacheLabel())); |
26 | 26 | } |
27 | 27 |
|
28 | | - Log::debug('Requested netifyd licence not found in cache, checking remote server.'); |
| 28 | + Log::debug('Requested netifyd license not found in cache, checking remote server.'); |
29 | 29 | // Check if the community license is on the remote server. |
30 | 30 | try { |
31 | | - $licences = $licenceProvider->listLicences(); |
| 31 | + $licenses = $licenseProvider->listLicenses(); |
32 | 32 | } catch (Exception $e) { |
33 | 33 | return response()->json(['message' => $e->getMessage()], 500); |
34 | 34 | } |
35 | | - $license = array_find($licences, fn ($item) => $item['issued_to'] == $licenceType->label()); |
| 35 | + $license = array_find($licenses, fn ($item) => $item['issued_to'] == $licenseType->label()); |
36 | 36 | // If it doesn't exist, create it. |
37 | 37 | if ($license == null) { |
38 | | - Log::debug('Netifyd licence not found on remote server, creating it.'); |
| 38 | + Log::debug('Netifyd license not found on remote server, creating it.'); |
39 | 39 | try { |
40 | | - $license = $licenceProvider->createLicence($licenceType); |
| 40 | + $license = $licenseProvider->createLicense($licenseType); |
41 | 41 | } catch (Exception $e) { |
42 | 42 | return response()->json(['message' => $e->getMessage()], 500); |
43 | 43 | } |
44 | 44 | } |
45 | 45 | // Got license, checking if everything is in place. |
46 | | - Log::debug('Netifyd licence recovered from remote server, checking if it can be renewed.'); |
| 46 | + Log::debug('Netifyd license recovered from remote server, checking if it can be renewed.'); |
47 | 47 | $expiration = $license['expire_at']['unix']; |
48 | 48 | $creation = $license['created_at']['unix']; |
49 | 49 | $renewalThreshold = ($expiration - $creation) / 2 + $creation; |
50 | 50 | $now = now()->unix(); |
51 | 51 | if ($renewalThreshold < $now) { |
52 | | - Log::debug('Netifyd licence can be renewed, renewing it.'); |
| 52 | + Log::debug('Netifyd license can be renewed, renewing it.'); |
53 | 53 | try { |
54 | | - $license = $licenceProvider->renewLicence($licenceType, $license['serial']); |
| 54 | + $license = $licenseProvider->renewLicense($licenseType, $license['serial']); |
55 | 55 | } catch (Exception $e) { |
56 | 56 | return response()->json(['message' => $e->getMessage()], 500); |
57 | 57 | } |
58 | 58 | } |
59 | 59 |
|
60 | 60 | $expiration = $license['expire_at']['unix']; |
61 | 61 | $creation = $license['created_at']['unix']; |
62 | | - Cache::put($licenceType->cacheLabel(), $license, ($expiration - $creation) / 2); |
| 62 | + Cache::put($licenseType->cacheLabel(), $license, ($expiration - $creation) / 2); |
63 | 63 |
|
64 | 64 | return response()->json($license); |
65 | 65 | } |
66 | 66 |
|
67 | | - public function enterprise(NetifydLicenceRepository $licenceProvider): JsonResponse |
| 67 | + public function enterprise(NetifydLicenseRepository $licenseProvider): JsonResponse |
68 | 68 | { |
69 | | - return $this->run($licenceProvider, NetifydLicenceType::ENTERPRISE); |
| 69 | + return $this->run($licenseProvider, NetifydLicenseType::ENTERPRISE); |
70 | 70 | } |
71 | 71 | } |
0 commit comments