Total Complexity | 8 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Coverage | 86.67% |
Changes | 0 |
1 | <?php |
||
12 | final class ORMBatchProcessor implements \IteratorAggregate |
||
13 | { |
||
14 | private iterable $items; |
||
15 | private EntityManagerInterface $em; |
||
16 | private int $chunkSize; |
||
17 | |||
18 | 16 | public function __construct(iterable $items, EntityManagerInterface $em, int $chunkSize = 100) |
|
27 | 16 | } |
|
28 | |||
29 | 16 | public function getIterator(): \Traversable |
|
30 | { |
||
31 | 16 | $logger = $this->em->getConfiguration()->getSQLLogger(); |
|
32 | 16 | $this->em->getConfiguration()->setSQLLogger(null); |
|
33 | 16 | $this->em->beginTransaction(); |
|
34 | |||
35 | 16 | $iteration = 0; |
|
36 | |||
37 | try { |
||
38 | 16 | foreach ($this->items as $key => $value) { |
|
39 | 16 | yield $key => $value; |
|
40 | |||
41 | 16 | $this->flushAndClearBatch(++$iteration); |
|
42 | } |
||
43 | } catch (\Throwable $exception) { |
||
44 | $this->em->rollback(); |
||
45 | |||
46 | throw $exception; |
||
47 | } |
||
48 | |||
49 | 16 | $this->flushAndClear(); |
|
50 | 16 | $this->em->commit(); |
|
51 | 16 | $this->em->getConfiguration()->setSQLLogger($logger); |
|
52 | 16 | } |
|
53 | |||
54 | 16 | private function flushAndClearBatch(int $iteration): void |
|
61 | 7 | } |
|
62 | |||
63 | 16 | private function flushAndClear(): void |
|
69 |