-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathRotateLicenseKey.php
More file actions
47 lines (38 loc) · 1.24 KB
/
RotateLicenseKey.php
File metadata and controls
47 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace App\Actions\Licenses;
use App\Models\License;
use App\Services\Anystack\Anystack;
class RotateLicenseKey
{
/**
* Rotate a license key by creating a new Anystack license
* and suspending the old one.
*/
public function handle(License $license): License
{
$newLicenseData = $this->createNewAnystackLicense($license);
$oldAnystackId = $license->anystack_id;
$license->update([
'anystack_id' => $newLicenseData['id'],
'key' => $newLicenseData['key'],
]);
$this->suspendOldAnystackLicense($license, $oldAnystackId);
return $license;
}
private function createNewAnystackLicense(License $license): array
{
return Anystack::api()
->licenses($license->anystack_product_id)
->create([
'policy_id' => $license->subscriptionType->anystackPolicyId(),
'contact_id' => $license->user->anystack_contact_id,
])
->json('data');
}
private function suspendOldAnystackLicense(License $license, string $oldAnystackId): void
{
Anystack::api()
->license($oldAnystackId, $license->anystack_product_id)
->suspend();
}
}