1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Api; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService; |
6
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
12
|
|
|
|
13
|
|
|
class GenerateKeyCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ApiKeyServiceInterface |
17
|
|
|
*/ |
18
|
|
|
private $apiKeyService; |
19
|
|
|
/** |
20
|
|
|
* @var TranslatorInterface |
21
|
|
|
*/ |
22
|
|
|
private $translator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* GenerateKeyCommand constructor. |
26
|
|
|
* @param ApiKeyServiceInterface|ApiKeyService $apiKeyService |
27
|
|
|
* @param TranslatorInterface $translator |
28
|
|
|
* |
29
|
|
|
* @Inject({ApiKeyService::class, "translator"}) |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator) |
32
|
|
|
{ |
33
|
2 |
|
$this->apiKeyService = $apiKeyService; |
34
|
2 |
|
$this->translator = $translator; |
35
|
2 |
|
parent::__construct(null); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function configure() |
39
|
|
|
{ |
40
|
2 |
|
$this->setName('api-key:generate') |
41
|
2 |
|
->setDescription($this->translator->translate('Generates a new valid API key.')) |
42
|
2 |
|
->addOption( |
43
|
2 |
|
'expirationDate', |
44
|
2 |
|
'e', |
45
|
2 |
|
InputOption::VALUE_OPTIONAL, |
46
|
2 |
|
$this->translator->translate('The date in which the API key should expire. Use any valid PHP format.') |
47
|
2 |
|
); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
2 |
|
$expirationDate = $input->getOption('expirationDate'); |
53
|
2 |
|
$apiKey = $this->apiKeyService->create(isset($expirationDate) ? new \DateTime($expirationDate) : null); |
54
|
2 |
|
$output->writeln($this->translator->translate('Generated API key') . sprintf(': <info>%s</info>', $apiKey)); |
55
|
2 |
|
} |
56
|
|
|
} |
57
|
|
|
|