feat: deactivate license

This commit is contained in:
Phan An 2024-01-08 14:02:26 +01:00
parent e5f049d785
commit 06a57a7a49
5 changed files with 81 additions and 11 deletions

View file

@ -21,15 +21,14 @@ class ActivateLicenseCommand extends Command
$this->components->info('Activating license…');
try {
$license = $this->licenseService->activateLicense($this->argument('key'));
$license = $this->licenseService->activate($this->argument('key'));
} catch (Throwable $e) {
$this->components->error($e->getMessage());
return self::FAILURE;
}
$this->output->success('License activated successfully. Thanks for supporting Koel!');
$this->output->success('Koel Plus activated! All Plus features are now available.');
$this->components->twoColumnDetail('License Key', $license->short_key);
$this->components->twoColumnDetail(
@ -38,7 +37,6 @@ class ActivateLicenseCommand extends Command
);
$this->components->twoColumnDetail('Expires On', 'Never ever');
$this->newLine();
return self::SUCCESS;

View file

@ -22,11 +22,11 @@ class CheckLicenseStatusCommand extends Command
$this->components->info('Checking your Koel Plus license status…');
try {
$status = $this->licenseService->getLicenseStatus(checkCache: false);
$status = $this->licenseService->getStatus(checkCache: false);
switch ($status->status) {
case LicenseStatus::STATUS_VALID:
$this->output->success('You have a valid Koel Plus license. Thanks for supporting Koel!');
$this->output->success('You have a valid Koel Plus license. All Plus features are enabled.');
$this->components->twoColumnDetail('License Key', $status->license->short_key);
$this->components->twoColumnDetail(
@ -40,12 +40,12 @@ class CheckLicenseStatusCommand extends Command
case LicenseStatus::STATUS_NO_LICENSE:
$this->components->info(
'No license found. Please consider purchasing one at https://plus.koel.dev.'
'No license found. You can purchase one at ' . config('lemonsqueezy.store_url')
);
break;
case LicenseStatus::STATUS_INVALID:
$this->components->error('Your license is invalid. Koel Plus features will not be available.');
$this->components->error('Your license is invalid. Plus features will not be available.');
break;
default:

View file

@ -0,0 +1,48 @@
<?php
namespace App\Console\Commands;
use App\Services\LicenseService;
use Illuminate\Console\Command;
use Throwable;
class DeactivateLicenseCommand extends Command
{
protected $signature = 'koel:license:deactivate';
protected $description = 'Deactivate the currently active Koel Plus license';
public function __construct(private LicenseService $plusService)
{
parent::__construct();
}
public function handle(): int
{
$status = $this->plusService->getStatus(checkCache: false);
if ($status->hasNoLicense()) {
$this->components->warn('No active Plus license found.');
return self::SUCCESS;
}
if (!$this->confirm('Are you sure you want to deactivate your Koel Plus license?')) {
$this->output->warning('License deactivation aborted.');
return self::SUCCESS;
}
$this->components->info('Deactivating your license…');
try {
$this->plusService->deactivateLicense($status->license);
$this->components->info('Koel Plus has been deactivated. Plus features are now disabled.');
return self::SUCCESS;
} catch (Throwable $e) {
$this->components->error('Failed to deactivate Koel Plus: ' . $e->getMessage());
return self::FAILURE;
}
}
}

View file

@ -20,7 +20,7 @@ class LicenseService
{
}
public function activateLicense(string $key): License
public function activate(string $key): License
{
try {
$response = $this->client->post('licenses/activate', [
@ -36,11 +36,30 @@ class LicenseService
} catch (ClientException $e) {
throw new FailedToActivateLicenseException(json_decode($e->getResponse()->getBody())->error, $e->getCode());
} catch (Throwable $e) {
Log::error($e);
throw FailedToActivateLicenseException::fromException($e);
}
}
public function getLicenseStatus(bool $checkCache = true): LicenseStatus
public function deactivateLicense(License $license): void
{
try {
$response = $this->client->post('licenses/deactivate', [
'license_key' => $license->key,
'instance_id' => $license->instance->id,
]);
if ($response->deactivated) {
$license->delete();
Cache::delete('license_status');
}
} catch (Throwable $e) {
Log::error($e);
throw $e;
}
}
public function getStatus(bool $checkCache = true): LicenseStatus
{
if ($checkCache && Cache::has('license_status')) {
return Cache::get('license_status');
@ -103,7 +122,7 @@ class LicenseService
public function isPlus(): bool
{
return $this->getLicenseStatus()->isValid();
return $this->getStatus()->isValid();
}
public function isCommunity(): bool

View file

@ -27,6 +27,11 @@ final class LicenseStatus
return $this->status === self::STATUS_VALID;
}
public function hasNoLicense(): bool
{
return $this->status === self::STATUS_NO_LICENSE;
}
public static function noLicense(): self
{
return new self(self::STATUS_NO_LICENSE, null);