EncryptCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BenTools\Shh\Command;
4
5
use BenTools\Shh\Shh;
6
use Symfony\Component\Console\Attribute\AsCommand;
7
use Symfony\Component\Console\Command\Command;
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\Style\SymfonyStyle;
12
13
#[AsCommand(
14
    name: 'shh:encrypt',
15
)]
16
final class EncryptCommand extends Command
17
{
18
    /**
19
     * @var Shh
20
     */
21
    private $shh;
22
23
    public function __construct(Shh $shh)
24
    {
25
        parent::__construct();
26
        $this->shh = $shh;
27
    }
28
29
    protected function configure(): void
30
    {
31
        $this
32
            ->setDescription('Encrypts a value.')
33
            ->addArgument('payload', InputArgument::OPTIONAL, 'The value to encrypt.')
34
        ;
35
    }
36
37
    protected function interact(InputInterface $input, OutputInterface $output): void
38
    {
39
        $io = new SymfonyStyle($input, $output);
40
41
        if (null === $input->getArgument('payload')) {
42
            $input->setArgument('payload', $io->askHidden(
43
                'Enter the value to encrypt:',
44
                function ($payload) {
45
46
                    if (!isset($payload[0])) {
47
                        throw new \InvalidArgumentException("Invalid value.");
48
                    }
49
50
                    return $payload;
51
                }
52
            ));
53
        }
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        $io = new SymfonyStyle($input, $output);
59
60
        if ($io->isQuiet()) {
61
            throw new \RuntimeException('This command is not intended to be run in quiet mode.');
62
        }
63
64
        $encrypted = $this->shh->encrypt($input->getArgument('payload'));
65
66
        if ($io->isVerbose()) {
67
            $io->comment('Here \'s your encrypted payload:');
68
        }
69
70
        $io->writeln($encrypted);
71
72
        return 0;
73
    }
74
}
75