63 lines
1.5 KiB
PHP
63 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Console\Commands;
|
||
|
|
||
|
use App\Models\ApiKey;
|
||
|
use Illuminate\Console\Command;
|
||
|
|
||
|
class DeleteApiKey extends Command
|
||
|
{
|
||
|
/**
|
||
|
* Error messages
|
||
|
*/
|
||
|
const MESSAGE_ERROR_INVALID_NAME = 'Invalid name.';
|
||
|
const MESSAGE_ERROR_NAME_DOES_NOT_EXIST = 'Name does not exist.';
|
||
|
/**
|
||
|
* The name and signature of the console command.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $signature = 'apikey:delete {name}';
|
||
|
/**
|
||
|
* The console command description.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $description = 'Delete an API key by name';
|
||
|
/**
|
||
|
* Execute the console command.
|
||
|
*/
|
||
|
public function handle()
|
||
|
{
|
||
|
$name = $this->argument('name');
|
||
|
$error = $this->validateName($name);
|
||
|
if ($error) {
|
||
|
$this->error($error);
|
||
|
return;
|
||
|
}
|
||
|
$confirmMessage = 'Are you sure you want to delete API key \'' . $name . '\'?';
|
||
|
if (!$this->confirm($confirmMessage)) {
|
||
|
return;
|
||
|
}
|
||
|
$key = ApiKey::where('name', $name)->first();
|
||
|
$key->delete();
|
||
|
$this->info('Deleted key: ' . $name);
|
||
|
}
|
||
|
/**
|
||
|
* Validate name
|
||
|
*
|
||
|
* @param string $name
|
||
|
* @return string
|
||
|
*/
|
||
|
protected function validateName($name)
|
||
|
{
|
||
|
if (!ApiKey::isValidName($name)) {
|
||
|
return self::MESSAGE_ERROR_INVALID_NAME;
|
||
|
}
|
||
|
if (!ApiKey::nameExists($name)) {
|
||
|
return self::MESSAGE_ERROR_NAME_DOES_NOT_EXIST;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|