Completed
Push — master ( e8e851...71a8c4 )
by BENOIT
06:28
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\DependencyInjection\ContainerInterface;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\HttpKernel\Kernel;
14
15
final class GenerateKeyPairCommand extends Command
16
{
17
    protected static $defaultName = 'shh:generate:keys';
18
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    /**
25
     * @var Shh
26
     */
27
    private $shh;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    private $fs;
33
34
    public function __construct(ContainerInterface $container, Shh $shh, Filesystem $fs)
35
    {
36
        parent::__construct();
37
        $this->container = $container;
38
        $this->shh = $shh;
39
        $this->fs = $fs;
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setDescription('Generate public/private keys.')
46
            ->addOption('passphrase', '', InputOption::VALUE_OPTIONAL, '(Optional) your passhrase.')
47
        ;
48
    }
49
50
    protected function interact(InputInterface $input, OutputInterface $output)
51
    {
52
        $io = new SymfonyStyle($input, $output);
53
54
        if (null === $input->getOption('passphrase')) {
55
            $input->setOption('passphrase', $io->askHidden(
56
                'Enter your new passphrase:',
57
                function ($passphrase) use ($io) {
58
59
                    if (0 === \strlen($passphrase)) {
60
                        return null;
61
                    }
62
63
                    if ($passphrase !== $io->askHidden('Confirm:')) {
64
                        throw new \InvalidArgumentException("Both passphrases don't match.");
65
                    }
66
67
                    return $passphrase;
68
                }
69
            ));
70
        }
71
    }
72
73
    /**
74
     * @param InputInterface  $input
75
     * @param OutputInterface $output
76
     * @return int|void|null
77
     */
78
    protected function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        $io = new SymfonyStyle($input, $output);
81
82
        $passphrase = $input->getOption('passphrase');
83
        $dir = $this->getDirectory();
84
85
        if ($this->fs->exists($dir.'/private.pem') || $this->fs->exists($dir.'/private.pem')) {
86
            $io->error(\sprintf("Keys are already defined in %s.", $dir));
87
88
            return 1;
89
        }
90
91
        list($publicKey, $privateKey) = Shh::generateKeyPair($passphrase);
92
93
        $this->fs->dumpFile($dir.'/private.pem', $privateKey);
94
        $this->fs->dumpFile($dir.'/public.pem', $publicKey);
95
96
        $io->success(\sprintf('%s was successfully created', $dir.'/private.pem'));
97
        $io->success(\sprintf('%s was successfully created', $dir.'/public.pem'));
98
99
        if (null !== $passphrase) {
100
            $io->comment('Don\'t forget to report your passphrase into the SHH_PASSPHRASE environment variable.');
101
        }
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    private function getDirectory()
108
    {
109
        if (Kernel::MAJOR_VERSION < 4) {
110
            return $this->container->getParameter('kernel.project_dir').'/app/config/shh';
111
        }
112
113
        return $this->container->getParameter('kernel.project_dir').'/config/shh';
114
    }
115
}
116