Completed
Push — master ( 2dff47...d26e62 )
by Oliver
03:56
created

ShareCommand::execute()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 79
c 0
b 0
f 0
ccs 0
cts 50
cp 0
rs 6.3115
cc 11
nc 17
nop 2
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: stefan
5
 * Date: 21.09.14
6
 * Time: 10:28
7
 */
8
9
namespace TQ\Shamir\Console;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Helper\FormatterHelper;
13
use Symfony\Component\Console\Helper\QuestionHelper;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\Question;
19
use TQ\Shamir\Secret;
20
use UnexpectedValueException;
21
22
class ShareCommand extends Command
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    protected function configure()
28
    {
29
        $this->setName('shamir:share')->setDescription('Create a shared secret')->addArgument(
30
            'secret',
31
            InputArgument::OPTIONAL,
32
            'The secret to share'
33
        )->addOption(
34
            'file',
35
            'f',
36
            InputOption::VALUE_OPTIONAL,
37
            'File containing secret'
38
        )->addOption(
39
            'shares',
40
            's',
41
            InputOption::VALUE_OPTIONAL,
42
            'The number of shared secrets to generate',
43
            3
44
        )->addOption(
45
            'threshold',
46
            't',
47
            InputOption::VALUE_OPTIONAL,
48
            'The minimum number of shared secrets required to recover',
49
            2
50
        );
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $secret = null;
59
60
        # check if data is given by STDIN
61
        $readStreams = [STDIN];
62
        $writeStreams = [];
63
        $exceptStreams = [];
64
        $streamCount = stream_select($readStreams, $writeStreams, $exceptStreams, 0);
65
66
        if ($streamCount === 1) {
67
            while (!feof(STDIN)) {
68
                $secret .= fread(STDIN, 1024);
69
            }
70
        } else {
71
            $file = $input->getOption('file');
72
            if ($file !== null) {
73
                # check for secret in file
74
                if (!is_readable($file)) {
75
                    $output->writeln('<error>ERROR: file "' . $file . '" is not readable.');
76
                    exit(1);
77
                }
78
79
                $secret = file_get_contents($file);
80
            }
81
        }
82
83
        if ($secret === null) {
84
            $secret = $input->getArgument('secret');
85
        }
86
87
        if (empty($secret)) {
88
            /** @var QuestionHelper $dialog */
89
            $helper = $this->getHelper('question');
90
91
            $question = new Question('<question>The secret to share</question>: ');
92
            $secret   = $helper->ask($input, $output, $question);
93
94
            $question = new Question(
95
                '<question>Number of shared secrets to create</question> <comment>[3]</comment>: ', 3
96
            );
97
            $question->setValidator(
98
                static function ($a) {
99
                    if (!is_int($a) && !ctype_digit($a)) {
100
                        throw new UnexpectedValueException('The number of shared secrets must be an integer');
101
                    }
102
103
                    return (int)$a;
104
                }
105
            );
106
            $shares = $helper->ask($input, $output, $question);
107
108
            $question = new Question(
109
                '<question>Number of shared secrets required</question> <comment>[2]</comment>: ', 2
110
            );
111
            $question->setValidator(
112
                function ($a) {
113
                    if (!is_int($a) && !ctype_digit($a)) {
114
                        throw new UnexpectedValueException('The number of shared secrets required must be an integer');
115
                    }
116
117
                    return (int)$a;
118
                }
119
            );
120
            $threshold = $helper->ask($input, $output, $question);
121
        } else {
122
            $shares    = $input->getOption('shares');
123
            $threshold = $input->getOption('threshold');
124
        }
125
126
        $shared = Secret::share($secret, $shares, $threshold);
127
128
        /** @var FormatterHelper $formatter */
129
        $formatter = $this->getHelper('formatter');
130
        $block     = $formatter->formatBlock($shared, 'info', true);
131
        $output->writeln($block);
132
133
        return 0;
134
    }
135
}
136