Skip to content

Commit

Permalink
Use Doctrine iterator to get all results
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfeinbier committed Sep 20, 2016
1 parent 8d8b8f8 commit 571afbd
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions Command/DoctrineEncryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Ambta\DoctrineEncryptBundle\Command;

use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,7 +28,9 @@ protected function configure()
$this
->setName('doctrine:encrypt:database')
->setDescription('Decrypt whole database on tables which are encrypted')
->addArgument("encryptor", InputArgument::OPTIONAL, "The encryptor u want to decrypt the database with");
->addArgument('encryptor', InputArgument::OPTIONAL, 'The encryptor u want to decrypt the database with')
->addArgument('batchSize', InputArgument::OPTIONAL, 'The update/flush batch size', 20);

}

/**
Expand All @@ -39,7 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
$question = $this->getHelper('question');
$subscriber = $this->getContainer()->get('ambta_doctrine_encrypt.subscriber');
$annotationReader = new AnnotationReader();
$annotationReader = $this->getContainer()->get('annotation_reader');
$batchSize = $input->getArgument('batchSize');

//Get list of supported encryptors
$supportedExtensions = DoctrineEncryptExtension::$supportedEncryptorClasses;
Expand Down Expand Up @@ -115,27 +119,56 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

//If class is not an superclass
$i = 0;
if (!$annotationReader->getClassAnnotation($reflectionClass, "Doctrine\ORM\Mapping\MappedSuperclass")) {

/**
* Get repository and entity Array
* @var \Doctrine\ORM\EntityRepository $repository
*/
$repository = $entityManager->getRepository($metaData->name);
$entityArray = $repository->findAll();

foreach($entityArray as $entity) {

$entity = $subscriber->processFields($entity);

//Persist and flush entity
$entityManager->persist($entity);
$entityManager->flush($entity);
$iterator = $this->getEntityIterator($entityManager, $metaData->name);
$totalCount = $this->getTableCount($entityManager, $metaData->name);

$output->writeln(sprintf('Processing <comment>%s</comment>', $metaData->name));
$progressBar = new ProgressBar($output, $totalCount);
foreach ($iterator as $row) {
$subscriber->processFields($row[0]);

if (($i % $batchSize) === 0) {
$entityManager->flush();
$entityManager->clear();
$progressBar->advance($batchSize);
}
$i++;
}

$progressBar->finish();
$output->writeln('');
$entityManager->flush();
}
}

//Say it is finished
$output->writeln("\nEncryption finished values encrypted: " . $subscriber->encryptCounter . " values.\nAll values are now encrypted.");
}

/**
* @param EntityManager $em
* @param $name
*
* @return \Doctrine\ORM\Internal\Hydration\IterableResult
*/
protected function getEntityIterator(EntityManager $em, $name)
{
$query = $em->createQuery(sprintf('SELECT o FROM %s o', $name));
return $query->iterate();
}

/**
* @param EntityManager $manager
* @param $name
*
* @return integer
*/
protected function getTableCount(EntityManager $manager, $name)
{
$query = $manager->createQuery(sprintf('SELECT COUNT(o) FROM %s o', $name));

return (int) $query->getSingleScalarResult();
}
}

0 comments on commit 571afbd

Please sign in to comment.