Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encrypt command #18

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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