feat: analyse import + all tests fixed
This commit is contained in:
parent
fbe9619224
commit
3170a7c60e
@@ -5,84 +5,24 @@ namespace App\Domain\Scraping\Infrastructure\Service;
|
||||
use App\Domain\Scraping\Domain\Contract\Service\CbzGeneratorInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\CbzGenerationRequest;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\CbzPath;
|
||||
use App\Domain\Shared\Domain\Contract\MangaPathManagerInterface;
|
||||
|
||||
readonly class CbzGenerator implements CbzGeneratorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $projectDir
|
||||
private MangaPathManagerInterface $mangaPathManager,
|
||||
) {
|
||||
}
|
||||
|
||||
public function generate(CbzGenerationRequest $request): CbzPath
|
||||
{
|
||||
$cbzPath = $this->generateCbzPath($request);
|
||||
$this->createCbzArchive($request->getFiles(), $cbzPath);
|
||||
$cbzPath = $this->mangaPathManager->buildChapterCbzPath(
|
||||
$request->getMangaTitle(),
|
||||
$request->getPublicationYear(),
|
||||
$request->getVolumeNumber(),
|
||||
$request->getChapterNumber(),
|
||||
);
|
||||
$this->mangaPathManager->createCbzArchive($request->getFiles(), $cbzPath);
|
||||
return new CbzPath($cbzPath);
|
||||
}
|
||||
|
||||
private function generateCbzPath(CbzGenerationRequest $request): string
|
||||
{
|
||||
$mangaDir = $this->createMangaDirectory(
|
||||
$this->slugify($request->getMangaTitle()),
|
||||
$request->getPublicationYear()
|
||||
);
|
||||
|
||||
$volumeDir = $this->createVolumeDirectory($mangaDir, $request->getVolumeNumber());
|
||||
|
||||
return sprintf(
|
||||
'%s/%s_vol%d_ch%s.cbz',
|
||||
$volumeDir,
|
||||
$this->slugify($request->getMangaTitle()),
|
||||
$request->getVolumeNumber(),
|
||||
$request->getChapterNumber()
|
||||
);
|
||||
}
|
||||
|
||||
private function createCbzArchive(array $files, string $cbzPath): void
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
if ($zip->open($cbzPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
|
||||
throw new \RuntimeException('Failed to create CBZ archive');
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (!file_exists($file)) {
|
||||
throw new \RuntimeException("File not found: $file");
|
||||
}
|
||||
$zip->addFile($file, basename($file));
|
||||
}
|
||||
|
||||
if (!$zip->close()) {
|
||||
throw new \RuntimeException('Failed to close CBZ archive');
|
||||
}
|
||||
}
|
||||
|
||||
private function createMangaDirectory(string $mangaSlug, string $publicationYear): string
|
||||
{
|
||||
$dir = sprintf('%s/public/cbz/%s', $this->projectDir, ucfirst($mangaSlug) . ' (' . $publicationYear . ')');
|
||||
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
|
||||
throw new \RuntimeException("Failed to create directory: $dir");
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
private function createVolumeDirectory(string $mangaDir, int $volumeNumber): string
|
||||
{
|
||||
$dir = sprintf('%s/volume_%02d', $mangaDir, $volumeNumber);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
|
||||
throw new \RuntimeException("Failed to create directory: $dir");
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
private function slugify(string $text): string
|
||||
{
|
||||
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
|
||||
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
|
||||
$text = preg_replace('~[^-\w]+~', '', $text);
|
||||
$text = trim($text, '-');
|
||||
$text = preg_replace('~-+~', '-', $text);
|
||||
$text = strtolower($text);
|
||||
return $text ?: 'n-a';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user