|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LycheeVerify\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use LycheeVerify\Contract\Status; |
| 7 | +use LycheeVerify\Validators\ValidatePro; |
| 8 | +use LycheeVerify\Validators\ValidateSupporter; |
| 9 | +use function Safe\json_encode; |
| 10 | + |
| 11 | +class CheckKeyCommand extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'verify:check-key {key : The license key to check}'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Check the sponsorship level for a given license key'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Execute the console command. |
| 29 | + */ |
| 30 | + public function handle(): int |
| 31 | + { |
| 32 | + if (!$this->hasArgument('key')) { |
| 33 | + $this->error('No key provided. Please provide a license key to check.'); |
| 34 | + |
| 35 | + return Command::FAILURE; |
| 36 | + } |
| 37 | + |
| 38 | + if (!is_string($this->argument('key'))) { |
| 39 | + $this->error('Invalid key format. The key must be a string.'); |
| 40 | + |
| 41 | + return Command::FAILURE; |
| 42 | + } |
| 43 | + |
| 44 | + $key = $this->argument('key'); |
| 45 | + |
| 46 | + $validateSupporter = new ValidateSupporter(); |
| 47 | + $validatePro = new ValidatePro(); |
| 48 | + |
| 49 | + // We use a dummy verifiable string for checking |
| 50 | + $verifiable = json_encode(['url' => '', 'email' => '']); |
| 51 | + |
| 52 | + // Check Pro edition first (highest tier) |
| 53 | + if ($validatePro->validate($verifiable, $key)) { |
| 54 | + $this->info('Key: ' . $key); |
| 55 | + $this->info('Sponsorship Level: ' . Status::PRO_EDITION->value . ' (Pro Edition)'); |
| 56 | + |
| 57 | + return Command::SUCCESS; |
| 58 | + } |
| 59 | + |
| 60 | + // Check Supporter edition |
| 61 | + if ($validateSupporter->validate($verifiable, $key)) { |
| 62 | + $this->info('Key: ' . $key); |
| 63 | + $this->info('Sponsorship Level: ' . Status::SUPPORTER_EDITION->value . ' (Supporter Edition)'); |
| 64 | + |
| 65 | + return Command::SUCCESS; |
| 66 | + } |
| 67 | + |
| 68 | + // Key doesn't match any known tier |
| 69 | + $this->warn('Key: ' . $key); |
| 70 | + $this->warn('Sponsorship Level: ' . Status::FREE_EDITION->value . ' (No valid sponsorship found)'); |
| 71 | + |
| 72 | + return Command::FAILURE; |
| 73 | + } |
| 74 | +} |
0 commit comments