Completed
Push — master ( f2605c...7239af )
by BENOIT
06:31 queued 10s
created

GenerateKeyPairCommand::getDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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