Skip to content

Commit

Permalink
Club setting itacImport item count now updated has it's consume by th…
Browse files Browse the repository at this point in the history
…e messenger
  • Loading branch information
froozeify committed Dec 23, 2024
1 parent 6e5e90f commit ee2ad08
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
49 changes: 49 additions & 0 deletions src/EventSubscriber/Kernel/MessengerSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\EventSubscriber\Kernel;

use App\Message\ItacMembersMessage;
use App\Service\ClubService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;

final class MessengerSubscriber implements EventSubscriberInterface {

public static function getSubscribedEvents(): array {
return [
WorkerMessageHandledEvent::class => [
['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());
}
}
}
8 changes: 3 additions & 5 deletions src/Message/ItacMembersMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/MessageHandler/ItacMembersMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/Service/ClubService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down Expand Up @@ -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();
}

}
2 changes: 1 addition & 1 deletion src/Service/ImportItacCsvService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ee2ad08

Please sign in to comment.