Skip to content

Commit

Permalink
Fix encrypt command
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhelias committed May 15, 2023
1 parent 80fbe4b commit f1509f2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Command/EncryptionDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$encryptDecrypt = $io->choice('Do you want to encrypt or decrypt data?', ['encrypt', 'decrypt']);
}

$kernel = $this->getApplication()->getKernel();
$io->comment(sprintf('<info>%s</info> data for the <info>%s</info> environment', ucfirst($encryptDecrypt), $kernel->getEnvironment()));

$metas = $this->entityManager->getMetadataFactory()->getAllMetadata();
$metaData = [];
foreach ($metas as $meta) {
Expand All @@ -87,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if($fieldName === null) {
$fieldName = $this->askFieldName($className, $input, $output);
$fieldName = $this->askFieldName($metaData[$className], $input, $output);
}

$blind = [];
Expand All @@ -96,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
[$result, $blind] = $this->encryptor->prepareForStorage((new \ReflectionClass($className))->newInstanceWithoutConstructor(), $fieldName, $value, (bool) $input->getOption('encrypt'));
} else {
$value = $input->getArgument('value') ?? $io->ask('What is the value of the entity you want to decrypt ?');
$result = $this->encryptor->decrypt($className->getName(), $fieldName, $value);
$result = $this->encryptor->decrypt($className, $fieldName, $value);
}

$io->success(sprintf('Result: [%s]', $result));
Expand All @@ -107,20 +110,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

private function askClassName(array $metaData, InputInterface $input, OutputInterface $output): ClassMetadata
private function askClassName(array $metaData, InputInterface $input, OutputInterface $output): string
{
$io = new SymfonyStyle($input, $output);
$question = new Question('Please enter an entity className'. PHP_EOL );
$question->setAutocompleterValues(array_keys($metaData));
$io->newLine();

$question->setNormalizer(static function ($value) use ($metaData) {
// $value can be null here
return $metaData[$value] ?? null;
$answer = $metaData[$value] ?? null;

return $answer instanceof ClassMetadata ? $answer->getName() : null;
});

$question->setValidator(static function ($answer) use ($metaData) {
if ($answer instanceof ClassMetadata === false) {
$question->setValidator(static function (?string $answer) {
if (null === $answer) {
throw new \RuntimeException(
'The className does not exists nor has encrypted fields in its definition.'
);
Expand Down

0 comments on commit f1509f2

Please sign in to comment.