GenerateSecretCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 12
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\UserInterface\Console\Security;
13
14
use Random\RandomException;
0 ignored issues
show
Bug introduced by
The type Random\RandomException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Console\Attribute\AsCommand;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
23
/**
24
 * GenerateSecretCommand
25
 *
26
 * @package Slick\WebStack\UserInterface\Console\Security
27
 */
28
#[AsCommand(
29
    name: 'security:generate-secret',
30
    description: 'Generate a secure encryption secret using a cryptographically secure method',
31
    aliases: ["secretgen"]
32
)]
33
final class GenerateSecretCommand extends Command
34
{
35
36
    /**
37
     * @inheritDoc
38
     */
39
    protected function configure(): void
40
    {
41
        $this
42
            ->addArgument('size', InputArgument::OPTIONAL, 'Length in bytes of the generated secret', 32)
43
            ->addOption(
44
                'encoding',
45
                'e',
46
                InputOption::VALUE_OPTIONAL,
47
                'Encoding of the generated secret. Possible values are: hex (hexadecimal), base64. Defaults to hex',
48
                'base64'
49
            )
50
            ->addUsage("security:generate-secret 24 -e base64")
51
        ;
52
    }
53
54
    /**
55
     * @throws RandomException
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output): int
58
    {
59
        $encoding = $input->getOption('encoding');
60
        $length = $this->size($input);
61
        $secret = random_bytes($length);
62
63
        $secret = match ($encoding) {
64
            'base64' => base64_encode($secret),
65
            default => bin2hex($secret),
66
        };
67
68
        $style = new SymfonyStyle($input, $output);
69
70
        $style->writeln('');
71
72
        $table = $style->createTable();
73
        $table->setHeaders(['Generated secret', 'Size']);
74
        $table->setHeaderTitle("Generate secret utility");
75
        $table->addRow([$secret, $length]);
76
        $table->render();
77
78
        return Command::SUCCESS;
79
    }
80
81
    /**
82
     * @return int<1, max>
83
     */
84
    private function size(InputInterface $input): int
85
    {
86
        $size = (int) $input->getArgument('size');
87
        if ($size > 0) {
88
            return $size;
89
        }
90
        return 1;
91
    }
92
}
93