- FileSystemManager.php refactoring of all write/read actions on filesystem

Deleted:
- old ToolbarManager.php
This commit is contained in:
Jérémy Guillot
2024-07-24 17:21:44 +02:00
parent 7068bd1a34
commit 7eba0981c8
10 changed files with 142 additions and 197 deletions

View File

@@ -4,23 +4,19 @@ namespace App\Service;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Manager\FileSystemManager;
use App\Repository\ChapterRepository;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\String\Slugger\SluggerInterface;
class MangaImportService
readonly class MangaImportService
{
private const string CBZ_DIRECTORY = 'public/cbz';
public function __construct(
private readonly string $projectDir,
private readonly EntityManagerInterface $entityManager,
private readonly ChapterRepository $chapterRepository,
private readonly Filesystem $filesystem,
private readonly SluggerInterface $slugger
private FileSystemManager $fileSystemManager,
private EntityManagerInterface $entityManager,
private ChapterRepository $chapterRepository,
private SluggerInterface $slugger
)
{
}
@@ -45,18 +41,17 @@ class MangaImportService
private function importVolume(Manga $manga, int $volume, string $tempFilePath): void
{
$permanentFileName = $this->createPermanentFileName($manga, $volume);
$mangaDirectory = $this->createMangaDirectory($manga);
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
$mangaDirectory = $this->fileSystemManager->createMangaDirectory($manga->getSlug(), $manga->getPublicationYear());
$volumeDirectory = $this->fileSystemManager->createVolumeDirectory($mangaDirectory, $volume);
$permanentFilePath = $volumeDirectory . '/' . $permanentFileName;
if ($this->filesystem->exists($permanentFilePath)) {
if ($this->fileSystemManager->fileExists($permanentFilePath)) {
throw new \RuntimeException("Un fichier pour ce volume existe déjà.");
}
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
$this->fileSystemManager->moveFile($tempFilePath, $permanentFilePath);
$this->updateVolumeChapters($manga, $volume, $permanentFilePath);
$this->entityManager->flush();
}
@@ -67,15 +62,15 @@ class MangaImportService
{
$volume = $chapter->getVolume();
$permanentFileName = $this->createPermanentFileName($manga, $volume, $chapter->getNumber());
$mangaDirectory = $this->createMangaDirectory($manga);
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
$mangaDirectory = $this->fileSystemManager->createMangaDirectory($manga->getSlug(), $manga->getPublicationYear());
$volumeDirectory = $this->fileSystemManager->createVolumeDirectory($mangaDirectory, $chapter->getVolume());
$permanentFilePath = $volumeDirectory . '/' . $permanentFileName;
if ($this->filesystem->exists($permanentFilePath)) {
if ($this->fileSystemManager->fileExists($permanentFilePath)) {
throw new \RuntimeException("Un fichier pour ce chapitre existe déjà.");
}
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
$this->fileSystemManager->moveFile($tempFilePath, $permanentFilePath);
$chapter->setCbzPath($permanentFilePath);
@@ -91,15 +86,6 @@ class MangaImportService
return $baseFileName . '.cbz';
}
private function createMangaDirectory(Manga $manga): string
{
$mangaYear = $manga->getPublicationYear() ?? 'unknown';
$directoryPath = self::CBZ_DIRECTORY . '/' . ucfirst($manga->getSlug()) . ' (' . $mangaYear . ')';
$this->filesystem->mkdir($directoryPath, 0755);
return $directoryPath;
}
private function updateVolumeChapters(Manga $manga, int $volume, string $cbzPath): void
{
$chapters = $this->chapterRepository->findBy([