GenerateCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 50
ccs 29
cts 29
cp 1
rs 10

2 Methods

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