Skip to content

Commit

Permalink
🐛 (Command) Fix id format in query for EncryptDatabaseCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
feymo committed Jun 11, 2023
1 parent ddc2a73 commit 8bf3a69
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions src/Command/EncryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SpecShaper\EncryptBundle\Command;

use Doctrine\Migrations\Query\Query;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -11,7 +10,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Command\Command;
use SpecShaper\EncryptBundle\Exception\EncryptException;
use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Annotations\Reader;
Expand All @@ -28,13 +26,11 @@ class EncryptDatabaseCommand extends Command

private array $encryptedFields = [];

private $plannedSql = [];

public function __construct(
private readonly Reader $annotationReader,
private readonly EncryptorInterface $encryptor,
private readonly ManagerRegistry $registry,
private array $annotationArray
private readonly array $annotationArray
)
{
parent::__construct();
Expand All @@ -46,7 +42,7 @@ protected function configure(): void
$this->addOption('manager', null,InputOption::VALUE_OPTIONAL,'Nominate the database connection manager name.');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{

$io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -77,15 +73,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->progressAdvance();
}

$io->progressFinish($tables);
$io->progressFinish();

return Command::SUCCESS;
}

private function decryptTable(string $tableName, array $fieldArray, string $direction){

$statement = '';

private function decryptTable(string $tableName, array $fieldArray, string $direction): void
{
// Get all the field names that have been encrypted as an array.
$select = array_keys($fieldArray);

Expand All @@ -97,9 +91,6 @@ private function decryptTable(string $tableName, array $fieldArray, string $dire
$encryptedEntityFields = $this->em->getConnection()->fetchAllAssociative($selectQuery);

foreach ($encryptedEntityFields as $entity) {

$sql = 'UPDATE ' . $tableName . ' SET ';

$decryptedFields = [];
foreach($fieldArray as $fieldName => $refProperty){

Expand All @@ -109,18 +100,11 @@ private function decryptTable(string $tableName, array $fieldArray, string $dire
$newValue = $this->encryptor->decrypt($entity[$fieldName]);
}

$decryptedFields[] = $fieldName . ' = "' . $newValue . '"';
$decryptedFields[$fieldName] = $newValue;
}

$sql .= implode( ', ', $decryptedFields);

$sql .= ' WHERE id = ' . $entity['id'] . ';';

$statement .= $sql;

$this->em->getConnection()->update($tableName, $decryptedFields, ['id' => $entity['id']]);
}

$this->em->getConnection()->executeStatement($statement);
}

private function getEncryptedFields(): array
Expand Down Expand Up @@ -180,12 +164,4 @@ private function isEncryptedProperty(\ReflectionProperty $refProperty)

return false;
}

protected function addSql(
string $sql,
array $params = [],
array $types = []
): void {
$this->plannedSql[] = new Query($sql, $params, $types);
}
}

0 comments on commit 8bf3a69

Please sign in to comment.