GenerateCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 21
ccs 17
cts 17
cp 1
crap 1
rs 9.3142
1
<?php
2
3
namespace AndrewCarterUK\CryptoKey\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
final class GenerateCommand extends Command
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 9
    protected function configure()
16
    {
17 9
        $this
18 9
            ->setName('generate')
19 9
            ->setDescription('Generate a key using a CSPRNG')
20 9
            ->addOption(
21 9
                'entropy',
22 9
                'e',
23 9
                InputOption::VALUE_OPTIONAL,
24 9
                'How many bytes of entropy do you want in your key? (defaults to 32 bytes or 256 bits)',
25
                32
26 9
            )
27 9
            ->addOption(
28 9
                'format',
29 9
                'f',
30 9
                InputOption::VALUE_OPTIONAL,
31 9
                'What format would do you want your key provided? (hex or base64, defaults to base64)',
32
                'base64'
33 9
            );
34
        ;
35 9
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42 6
        $entropy = $input->getOption('entropy');
43 6
        $format = $input->getOption('format');
44
45
        $formatters = [
46 6
            'base64' => 'base64_encode',
47 6
            'hex' => 'bin2hex',
48 6
        ];
49
50 6
        if (!isset($formatters[$format])) {
51 3
            throw new \InvalidArgumentException('Unrecognized format: ' . $format);
52
        }
53
54 3
        $data = random_bytes($entropy);
55 3
        $result = $formatters[$format]($data);
56
57 3
        $output->writeln($result);
58 3
    }
59
}
60
61