Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

GenerateKeyCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10
wmc 4
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
A execute() 0 6 2
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