- AdditionnalData for buttons
- refresh manga metadata and chapters
This commit is contained in:
Jérémy Guillot
2024-07-05 19:03:16 +02:00
parent 3012adfee7
commit 586ebdb126
14 changed files with 283 additions and 107 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace App\MessageHandler;
use App\Message\RefreshMetadata;
use App\Repository\MangaRepository;
use App\Service\MangadexProvider;
use App\Service\NotificationService;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
readonly class RefreshMetadataHandler
{
public function __construct(
private MangaRepository $mangaRepository,
private MangadexProvider $mangadexProvider,
private EntityManagerInterface $entityManager,
private NotificationService $notificationService
)
{
}
public function __invoke(RefreshMetadata $message): void
{
$manga = $this->mangaRepository->find($message->getMangaId());
if (!$manga) {
return;
}
$lastChapters = $this->mangadexProvider->addAllChaptersToManga($manga);
try {
foreach ($manga->getChapters() as $chapter) {
$this->entityManager->persist($chapter);
}
$this->entityManager->persist($manga);
$this->entityManager->flush();
} catch (\Exception $e) {
if ($e instanceof UniqueConstraintViolationException) {
$this->notificationService->sendUpdate(['status' => 'error', 'message' => 'An error occurred while refreshing ' . $manga->getTitle() . '.']);
return;
}
}
$this->notificationService->sendUpdate(['status' => 'success', 'message' => $manga->getTitle() . ' refreshed, ' . count($lastChapters) . ' new chapters added.']);
}
}