Basic short-úrl import implementation

This commit is contained in:
Alejandro Celaya
2020-10-24 13:55:54 +02:00
parent 554d9b092f
commit ec3e7212b2
6 changed files with 100 additions and 18 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Util;
use Doctrine\ORM\EntityManagerInterface;
use IteratorAggregate;
use Throwable;
/**
* Inspired by ocramius/doctrine-batch-utils https://github.com/Ocramius/DoctrineBatchUtils
*/
class DoctrineBatchIterator implements IteratorAggregate
{
private iterable $resultSet;
private EntityManagerInterface $em;
private int $batchSize;
public function __construct(iterable $resultSet, EntityManagerInterface $em, int $batchSize)
{
$this->resultSet = $resultSet;
$this->em = $em;
$this->batchSize = $batchSize;
}
/**
* @throws Throwable
*/
public function getIterator(): iterable
{
$iteration = 0;
$resultSet = $this->resultSet;
$this->em->beginTransaction();
try {
foreach ($resultSet as $key => $value) {
$iteration++;
yield $key => $value;
$this->flushAndClearBatch($iteration);
}
} catch (Throwable $e) {
$this->em->rollback();
throw $e;
}
$this->flushAndClearEntityManager();
$this->em->commit();
}
private function flushAndClearBatch(int $iteration): void
{
if ($iteration % $this->batchSize) {
return;
}
$this->flushAndClearEntityManager();
}
private function flushAndClearEntityManager(): void
{
$this->em->flush();
$this->em->clear();
}
}