GenerateKeyPairCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BenTools\Shh\Command;
4
5
use BenTools\Shh\Shh;
6
use Symfony\Component\Console\Attribute\AsCommand;
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 Symfony\Component\Console\Style\SymfonyStyle;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
#[AsCommand(
15
    name: 'shh:generate:keys',
16
)]
17
final class GenerateKeyPairCommand extends Command
18
{
19
    /**
20
     * @var string
21
     */
22
    private $keysDirectory;
23
24
    /**
25
     * @var Filesystem
26
     */
27
    private $fs;
28
29
    public function __construct(string $keysDirectory, Filesystem $fs)
30
    {
31
        parent::__construct();
32
        $this->keysDirectory = $keysDirectory;
33
        $this->fs = $fs;
34
    }
35
36
    protected function configure(): void
37
    {
38
        $this
39
            ->setDescription('Generate public/private keys.')
40
            ->addOption('passphrase', '', InputOption::VALUE_OPTIONAL, '(Optional) your passhrase.')
41
        ;
42
    }
43
44
    protected function interact(InputInterface $input, OutputInterface $output): void
45
    {
46
        $io = new SymfonyStyle($input, $output);
47
48
        if (null === $input->getOption('passphrase')) {
49
            $input->setOption('passphrase', $io->askHidden(
50
                'Enter your new passphrase:',
51
                function ($passphrase) use ($io) {
52
53
                    if (0 === \strlen($passphrase)) {
54
                        return null;
55
                    }
56
57
                    if ($passphrase !== $io->askHidden('Confirm:')) {
58
                        throw new \InvalidArgumentException("Both passphrases don't match.");
59
                    }
60
61
                    return $passphrase;
62
                }
63
            ));
64
        }
65
    }
66
67
    protected function execute(InputInterface $input, OutputInterface $output): int
68
    {
69
        $io = new SymfonyStyle($input, $output);
70
71
        $passphrase = $input->getOption('passphrase');
72
        $dir = $this->keysDirectory;
73
74
        if ($this->fs->exists($dir . '/private.pem') || $this->fs->exists($dir . '/public.pem')) {
75
            $io->error(\sprintf("Keys are already defined in %s.", $dir));
76
77
            return 1;
78
        }
79
80
        list($publicKey, $privateKey) = Shh::generateKeyPair($passphrase);
81
82
        $this->fs->dumpFile($dir . '/private.pem', $privateKey);
83
        $this->fs->dumpFile($dir . '/public.pem', $publicKey);
84
85
        $io->success(\sprintf('%s was successfully created', $dir . '/private.pem'));
86
        $io->success(\sprintf('%s was successfully created', $dir . '/public.pem'));
87
88
        if (null !== $passphrase) {
89
            $io->comment('Don\'t forget to report your passphrase into the SHH_PASSPHRASE environment variable.');
90
        }
91
92
        return 0;
93
    }
94
}
95