- ContentSource handling in message
- ContentSource list, add/update ui
- nextPageSelector and imageSelector can be null
- cleanup
This commit is contained in:
Jérémy Guillot
2024-06-30 20:47:27 +02:00
parent ba30d3102d
commit 3012adfee7
24 changed files with 762 additions and 707 deletions

View File

@@ -1,111 +0,0 @@
<?php
namespace App\Twig\Components;
use App\Entity\Manga;
use App\Service\MangadexProvider;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent]
class NewMangaForm
{
use ComponentToolsTrait;
use DefaultActionTrait;
public ?Manga $manga = null;
#[LiveProp(writable: true)]
public array $mangaData = [];
#[LiveProp(writable: true)]
public ?int $index = 0;
public function __construct(private UrlGeneratorInterface $urlGenerator)
{
}
public function mount(Manga $manga): void
{
$this->manga = $manga;
$this->mangaData = [
'title' => $manga->getTitle(),
'slug' => $manga->getSlug(),
'description' => $manga->getDescription(),
'imageUrl' => $manga->getImageUrl(),
'status' => $manga->getStatus(),
'genres' => $manga->getGenres(),
'author' => $manga->getAuthor(),
'publicationYear' => $manga->getPublicationYear(),
'rating' => $manga->getRating(),
'externalId' => $manga->getExternalId(),
];
}
#[LiveAction]
public function saveManga(EntityManagerInterface $entityManager, MangadexProvider $mangadexProvider): Response
{
$manga = new Manga();
$manga->setTitle($this->mangaData['title'])
->setSlug($this->mangaData['slug'])
->setDescription($this->mangaData['description'])
->setImageUrl($this->mangaData['imageUrl'])
->setStatus($this->mangaData['status'])
->setGenres($this->mangaData['genres'])
->setAuthor($this->mangaData['author'])
->setPublicationYear($this->mangaData['publicationYear'])
->setRating($this->mangaData['rating'])
->setExternalId($this->mangaData['externalId']);
$mangaFeed = $mangadexProvider->getFeed($manga);
$mangaAggregate = $mangadexProvider->getMangaAggregate($manga);
$allChapters = array_merge($mangaFeed, $mangaAggregate);
$mergedChapters = [];
foreach ($allChapters as $chapter) {
$number = $chapter->getNumber();
if (isset($mergedChapters[$number])) {
$existingChapter = $mergedChapters[$number];
if (!empty($chapter->getExternalId()) ||
(empty($existingChapter->getExternalId()) && !strpos($chapter->getTitle(), 'Chapter ') == 0)) {
$mergedChapters[$number] = $chapter;
}
} else {
$mergedChapters[$number] = $chapter;
}
}
foreach($mergedChapters as $chapter) {
$manga->addChapter($chapter);
}
$mangaChapterUrl = $this->urlGenerator->generate('app_manga_show', ['mangaSlug' => $manga->getSlug()]);
try {
foreach ($manga->getChapters() as $chapter) {
$entityManager->persist($chapter);
}
$entityManager->persist($manga);
$entityManager->flush();
} catch (\Exception $e) {
if ($e instanceof UniqueConstraintViolationException) {
return new RedirectResponse($mangaChapterUrl);
}
throw $e;
}
return new RedirectResponse($mangaChapterUrl);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Twig\Extension;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('get_placeholder', [$this, 'getPlaceholder']),
];
}
public function getPlaceholder(string $fieldName): string
{
return match ($fieldName) {
'baseUrl' => 'https://example.com',
'imageSelector' => '.manga-image img',
'chapterUrlFormat' => 'https://example.com/manga/{slug}/chapter-{number}',
'nextPageSelector' => '.next-page',
'scrapingType' => 'Select scraping type',
default => '',
};
}
}