From f1509f269a1e499b9a537fe62169cd2863f1aa32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Mon, 15 May 2023 11:04:49 +0200 Subject: [PATCH] Fix encrypt command --- src/Command/EncryptionDataCommand.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Command/EncryptionDataCommand.php b/src/Command/EncryptionDataCommand.php index 74addc3..5a46f42 100644 --- a/src/Command/EncryptionDataCommand.php +++ b/src/Command/EncryptionDataCommand.php @@ -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('%s data for the %s environment', ucfirst($encryptDecrypt), $kernel->getEnvironment())); + $metas = $this->entityManager->getMetadataFactory()->getAllMetadata(); $metaData = []; foreach ($metas as $meta) { @@ -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 = []; @@ -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)); @@ -107,7 +110,7 @@ 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 ); @@ -115,12 +118,13 @@ private function askClassName(array $metaData, InputInterface $input, OutputInte $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.' );