RecoverCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 17
c 2
b 0
f 2
dl 0
loc 39
ccs 0
cts 19
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 22 3
A configure() 0 6 1
1
<?php
2
3
namespace TQ\Shamir\Console;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\FormatterHelper;
7
use Symfony\Component\Console\Helper\QuestionHelper;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
use TQ\Shamir\Secret;
13
14
class RecoverCommand extends Command
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    protected function configure(): void
20
    {
21
        $this->setName('shamir:recover')->setDescription('Recover a shared secret')->addArgument(
22
            'shares',
23
            InputArgument::IS_ARRAY,
24
            'Add the shared secrets'
25
        );
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33
        /** @var array $shares */
34
        $shares = $input->getArgument('shares');
35
        if (empty($shares)) {
36
            /** @var QuestionHelper $dialog */
37
            $helper   = $this->getHelper('question');
38
            $question = new Question('<question>Shared secret</question> <comment>[empty to stop]</comment>: ');
39
            $shares   = [];
40
            while (($share = trim($helper->ask($input, $output, $question))) !== '') {
41
                $shares[] = $share;
42
            }
43
        }
44
45
        $shared = Secret::recover($shares);
46
47
        /** @var FormatterHelper $formatter */
48
        $formatter = $this->getHelper('formatter');
49
        $block     = $formatter->formatBlock($shared, 'info');
50
        $output->writeln($block);
51
52
        return 0;
53
    }
54
}
55