1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Api; |
6
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes; |
8
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; |
9
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
class DisableKeyCommand extends Command |
19
|
|
|
{ |
20
|
|
|
public const NAME = 'api-key:disable'; |
21
|
|
|
|
22
|
|
|
private ApiKeyServiceInterface $apiKeyService; |
23
|
|
|
|
24
|
2 |
|
public function __construct(ApiKeyServiceInterface $apiKeyService) |
25
|
|
|
{ |
26
|
2 |
|
parent::__construct(); |
27
|
2 |
|
$this->apiKeyService = $apiKeyService; |
28
|
2 |
|
} |
29
|
|
|
|
30
|
2 |
|
protected function configure(): void |
31
|
|
|
{ |
32
|
2 |
|
$this->setName(self::NAME) |
33
|
2 |
|
->setDescription('Disables an API key.') |
34
|
2 |
|
->addArgument('apiKey', InputArgument::REQUIRED, 'The API key to disable'); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
38
|
|
|
{ |
39
|
2 |
|
$apiKey = $input->getArgument('apiKey'); |
40
|
2 |
|
$io = new SymfonyStyle($input, $output); |
41
|
|
|
|
42
|
|
|
try { |
43
|
2 |
|
$this->apiKeyService->disable($apiKey); |
|
|
|
|
44
|
1 |
|
$io->success(sprintf('API key "%s" properly disabled', $apiKey)); |
|
|
|
|
45
|
1 |
|
return ExitCodes::EXIT_SUCCESS; |
46
|
1 |
|
} catch (InvalidArgumentException $e) { |
47
|
1 |
|
$io->error($e->getMessage()); |
48
|
1 |
|
return ExitCodes::EXIT_FAILURE; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|