Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

UtilitiesBundle/Command/CipherCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
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