Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

CipherCommand::execute()   C

Complexity

Conditions 15
Paths 22

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 0
cts 53
cp 0
rs 5.0169
c 0
b 0
f 0
cc 15
nc 22
nop 2
crap 240

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
namespace Kunstmaan\UtilitiesBundle\Command;
4
5
use Kunstmaan\UtilitiesBundle\Helper\Cipher\CipherInterface;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ChoiceQuestion;
10
use Symfony\Component\Console\Question\Question;
11
use Symfony\Component\Filesystem\Filesystem;
12
13
/**
14
 * @final since 5.1
15
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
16
 */
17
class CipherCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * @var CipherInterface
21
     */
22
    private $cipher;
23
24
    private static $methods = [
25
        0 => 'Encrypt text',
26
        1 => 'Decrypt text',
27
        2 => 'Encrypt file',
28
        3 => 'Decrypt file',
29
    ];
30
31
    /**
32
     * @param CipherInterface|null $cipher
33
     */
34
    public function __construct(/* CipherInterface */ $cipher = null)
35
    {
36
        parent::__construct();
37
38
        if (!$cipher instanceof CipherInterface) {
39
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
40
41
            $this->setName(null === $cipher ? 'kuma:cipher' : $cipher);
42
43
            return;
44
        }
45
46
        $this->cipher = $cipher;
47
    }
48
49
    protected function configure()
50
    {
51
        $this->setName('kuma:cipher')->setDescription('Cipher utilities commands.');
52
    }
53
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        if (null === $this->cipher) {
57
            $this->cipher = $this->getContainer()->get('kunstmaan_utilities.cipher');
58
        }
59
        $helper = $this->getHelper('question');
60
61
        $question = new ChoiceQuestion(
62
            'Please select the method you want to use',
63
            self::$methods,
64
            0
65
        );
66
67
        $question->setErrorMessage('Method %s is invalid.');
68
        $method = $helper->ask($input, $output, $question);
69
        $method = array_search($method, self::$methods, true);
70
        switch ($method) {
71
            case 0:
72
            case 1:
73
                $question = new Question('Please enter the text: ');
74
                $question->setValidator(function ($value) {
75
                    if (trim($value) === '') {
76
                        throw new \Exception('The text cannot be empty');
77
                    }
78
79
                    return $value;
80
                });
81
                $question->setMaxAttempts(3);
82
                $text = $helper->ask($input, $output, $question);
83
                $text = $method === 0 ? $this->cipher->encrypt($text) : $this->cipher->decrypt($text);
84
                $output->writeln(sprintf('Result: %s', $text));
85
86
                break;
87
            case 2:
88
            case 3:
89
            $fs = new Filesystem();
90
91
            $question = new Question('Please enter the input file path: ');
92
                $question->setValidator(function ($value) use ($fs) {
93
                    if (trim($value) === '') {
94
                        throw new \Exception('The input file path cannot be empty');
95
                    }
96
97
                    if (false === $fs->exists($value)) {
98
                        throw new \Exception('The input file must exists');
99
                    }
100
101
                    if (is_dir($value)) {
102
                        throw new \Exception('The input file cannot be a dir');
103
                    }
104
105
                    return $value;
106
                });
107
                $question->setMaxAttempts(3);
108
                $inputFilePath = $helper->ask($input, $output, $question);
109
110
                $question = new Question('Please enter the output file path: ');
111
                $question->setValidator(function ($value) {
112
                    if (trim($value) === '') {
113
                        throw new \Exception('The output file path cannot be empty');
114
                    }
115
116
                    if (is_dir($value)) {
117
                        throw new \Exception('The output file path cannot be a dir');
118
                    }
119
120
                    return $value;
121
                });
122
                $question->setMaxAttempts(3);
123
                $outputFilePath = $helper->ask($input, $output, $question);
124
125
                if ($method === 2) {
126
                    $this->cipher->encryptFile($inputFilePath, $outputFilePath);
127
                } else {
128
                    if (false === $fs->exists($outputFilePath)) {
129
                        $fs->touch($outputFilePath);
130
                    }
131
                    $this->cipher->decryptFile($inputFilePath, $outputFilePath);
132
                }
133
134
                $output->writeln(sprintf('Check "%s" to see result', $outputFilePath));
135
136
                break;
137
        }
138
139
        return 0;
140
    }
141
}
142