Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
18.18% |
2 / 11 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| IndexAfterResponseIsSentStrategy | |
18.18% |
2 / 11 |
|
50.00% |
2 / 4 |
18.69 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSubscribedEvents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateIndex | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onKernelTerminate | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\DoctrineEntityDatalayer\IndexStrategy; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Apie\DoctrineEntityDatalayer\EntityReindexer; |
| 6 | use Apie\StorageMetadataBuilder\Interfaces\HasIndexInterface; |
| 7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 8 | use Symfony\Component\HttpKernel\KernelEvents; |
| 9 | |
| 10 | class IndexAfterResponseIsSentStrategy implements IndexStrategyInterface, EventSubscriberInterface |
| 11 | { |
| 12 | /** @var array<int, array{0: HasIndexInterface, 1: EntityInterface}> $todo */ |
| 13 | private array $todo = []; |
| 14 | public function __construct(private readonly EntityReindexer $entityReindexer) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | public static function getSubscribedEvents(): array |
| 19 | { |
| 20 | return [KernelEvents::TERMINATE => ['onKernelTerminate']]; |
| 21 | } |
| 22 | |
| 23 | public function updateIndex( |
| 24 | HasIndexInterface $doctrineEntity, |
| 25 | EntityInterface $entity |
| 26 | ): void { |
| 27 | $this->todo[] = [$doctrineEntity, $entity]; |
| 28 | } |
| 29 | |
| 30 | public function onKernelTerminate(): void |
| 31 | { |
| 32 | while (!empty($this->todo)) { |
| 33 | $call = array_shift($this->todo); |
| 34 | $this->entityReindexer->updateIndex( |
| 35 | ...[ |
| 36 | ...$call, |
| 37 | !empty($this->todo) |
| 38 | ] |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | } |