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

GenerateKeyCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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