AddKeyCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Command\Storage;
6
7
use Damax\Bundle\ApiAuthBundle\Key\Factory;
8
use Damax\Bundle\ApiAuthBundle\Key\Storage\Writer as Storage;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
final class AddKeyCommand extends Command
16
{
17
    protected static $defaultName = 'damax:api-auth:storage:add-key';
18
19
    private $factory;
20
    private $storage;
21
22
    public function __construct(Factory $factory, Storage $storage)
23
    {
24
        parent::__construct();
25
26
        $this->factory = $factory;
27
        $this->storage = $storage;
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setDescription('Add api key to storage.')
34
            ->addArgument('identity', InputArgument::REQUIRED, 'Identity of the key.')
35
            ->addArgument('ttl', InputArgument::OPTIONAL, 'Time to live in free form.', '1 week')
36
        ;
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $io = new SymfonyStyle($input, $output);
42
43
        if (false === $ttl = strtotime((string) $input->getArgument('ttl'), 0)) {
44
            $io->error('Invalid ttl.');
45
46
            return 1;
47
        }
48
49
        $key = $this->factory->createKey((string) $input->getArgument('identity'), $ttl);
50
51
        $this->storage->add($key);
52
53
        $io->success('Key: ' . (string) $key);
54
    }
55
}
56