diff --git a/src/EventSubscriber/Kernel/MessengerSubscriber.php b/src/EventSubscriber/Kernel/MessengerSubscriber.php new file mode 100644 index 00000000..11443e10 --- /dev/null +++ b/src/EventSubscriber/Kernel/MessengerSubscriber.php @@ -0,0 +1,49 @@ + [ + ['messageHandled', 10], + ], + WorkerMessageFailedEvent::class => [ + ['messageFailed', 10], + ], + ]; + } + + public function __construct( + private readonly ClubService $clubService, + ) { + } + + public function messageHandled(WorkerMessageHandledEvent $event): void { + $message = $event->getEnvelope()->getMessage(); + $this->consumeMessage($message); + } + + public function messageFailed(WorkerMessageFailedEvent $event): void { + // We only update the count for failed message + if ($event->willRetry()) { + return; + } + + $message = $event->getEnvelope()->getMessage(); + $this->consumeMessage($message); + } + + private function consumeMessage(object $message): void { + if ($message instanceof ItacMembersMessage) { + $this->clubService->consumeItacImport($message->getClubUuid()); + } + } +} diff --git a/src/Message/ItacMembersMessage.php b/src/Message/ItacMembersMessage.php index 6f648f99..81027a0d 100644 --- a/src/Message/ItacMembersMessage.php +++ b/src/Message/ItacMembersMessage.php @@ -2,19 +2,17 @@ namespace App\Message; -use App\Entity\Club; - class ItacMembersMessage { //TODO: Add a message batch id, so we can log in db after the success/warnings with some stats public function __construct( - private readonly Club $club, + private readonly string $clubUuid, private readonly array $records, ) { } - public function getClub(): Club { - return $this->club; + public function getClubUuid(): string { + return $this->clubUuid; } public function getRecords(): array { diff --git a/src/MessageHandler/ItacMembersMessageHandler.php b/src/MessageHandler/ItacMembersMessageHandler.php index 23e22ec1..488fa78a 100644 --- a/src/MessageHandler/ItacMembersMessageHandler.php +++ b/src/MessageHandler/ItacMembersMessageHandler.php @@ -51,8 +51,7 @@ public function __invoke(ItacMembersMessage $message): void { // 'success' => 0 // ]; - // We can get a Proxy object, so we load it to have the correct Club object - $club = $this->clubRepository->findOneByUuid($message->getClub()->getUuid()); + $club = $this->clubRepository->findOneByUuid($message->getClubUuid()); foreach ($message->getRecords() as $record) { // We get the user, if he exists diff --git a/src/Service/ClubService.php b/src/Service/ClubService.php index 6b7503a6..60c896e5 100644 --- a/src/Service/ClubService.php +++ b/src/Service/ClubService.php @@ -13,8 +13,9 @@ class ClubService { public function __construct( - private EntityManagerInterface $entityManager, - private UserRepository $userRepository, + private readonly EntityManagerInterface $entityManager, + private readonly UserRepository $userRepository, + private readonly ClubRepository $clubRepository, ) { } @@ -84,4 +85,18 @@ public function setItacImport(Club $club, int $numberOfBatches): void { $this->entityManager->flush(); } + public function consumeItacImport(string $clubUuid): void { + $club = $this->clubRepository->findOneByUuid($clubUuid); + if (!$club instanceof Club) { + return; + } + + $clubSettings = $club->getSettings(); + $clubSettings + ->setItacImportRemaining($clubSettings->getItacImportRemaining() - 1); + + $this->entityManager->persist($clubSettings); + $this->entityManager->flush(); + } + } diff --git a/src/Service/ImportItacCsvService.php b/src/Service/ImportItacCsvService.php index 5ea7f200..b0d21ca7 100644 --- a/src/Service/ImportItacCsvService.php +++ b/src/Service/ImportItacCsvService.php @@ -41,7 +41,7 @@ public function importFromFile(Club $club, string $filename): int { $chunk[$key][$this->convert($k)] = $this->convert($v); } } - $this->bus->dispatch(new ItacMembersMessage($club, $chunk)); + $this->bus->dispatch(new ItacMembersMessage($club->getUuid(), $chunk)); } return count($array);