Skip to content

Commit

Permalink
fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Jan 24, 2024
1 parent b1b5a6f commit 2f5d00b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
10 changes: 6 additions & 4 deletions src/Command/BooksScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace App\Command;

use App\Repository\BookRepository;
use App\Service\BookFileSystemManager;
use App\Service\BookManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -38,8 +36,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->writeln('Scanning books directory');

if($input->getOption('book-path') !== null) {
$info = new \SplFileInfo($input->getOption('book-path'));
if ($input->getOption('book-path') !== null) {
$path = $input->getOption('book-path');
if (!is_string($path)) {
throw new \Exception('Invalid path');
}
$info = new \SplFileInfo($path);
$book = $this->bookManager->consumeBook($info);
$this->entityManager->persist($book);
$this->entityManager->flush();
Expand Down
21 changes: 9 additions & 12 deletions src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function index(Request $request, Book $book, string $slug, BookSuggestion
$sameAuthorBooks = $bookRepository->getWithSameAuthors($book, 6);

$calculatedPath = $fileSystemManager->getCalculatedFilePath($book, false).$book->getBookFilename();
$needsRelocation = $fileSystemManager->getCalculatedFilePath($book, false).$book->getBookFilename()!==$book->getBookPath().$book->getBookFilename();
$needsRelocation = $fileSystemManager->getCalculatedFilePath($book, false).$book->getBookFilename() !== $book->getBookPath().$book->getBookFilename();

return $this->render('book/index.html.twig', [
'book' => $book,
Expand Down Expand Up @@ -145,18 +145,18 @@ public function consume(Request $request, BookFileSystemManager $fileSystemManag
$bookFiles = iterator_to_array($bookFiles);

$consume = $request->get('consume');
if($consume!==null) {
if ($consume !== null) {
foreach ($bookFiles as $bookFile) {
if($bookFile->getRealPath()!==$consume) {
if ($bookFile->getRealPath() !== $consume) {
continue;
}
$childProcess = new Process(['/var/www/html/bin/console', 'books:scan','--book-path', $bookFile->getRealPath()]);
$childProcess = new Process(['/var/www/html/bin/console', 'books:scan', '--book-path', $bookFile->getRealPath()]);

$childProcess->start();

$childProcess->wait();

$childProcess = new Process(['/var/www/html/bin/console', 'books:extract-cover','all']);
$childProcess = new Process(['/var/www/html/bin/console', 'books:extract-cover', 'all']);

$childProcess->start();

Expand All @@ -165,18 +165,18 @@ public function consume(Request $request, BookFileSystemManager $fileSystemManag
$this->addFlash('success', 'Book '.$bookFile->getFilename().' consumed');

return $this->redirectToRoute('app_book_consume');

}
}

$delete = $request->get('delete');
if($delete!==null) {
if ($delete !== null) {
foreach ($bookFiles as $bookFile) {
if($bookFile->getRealPath()!==$delete) {
if ($bookFile->getRealPath() !== $delete) {
continue;
}
unlink($bookFile->getRealPath());
$this->addFlash('success', 'Book '.$bookFile->getFilename().' deleted');

return $this->redirectToRoute('app_book_consume');
}
}
Expand All @@ -186,7 +186,6 @@ public function consume(Request $request, BookFileSystemManager $fileSystemManag
]);
}


#[Route('/relocate/{id}/files', name: 'app_book_relocate')]
public function relocate(Book $book, BookFileSystemManager $fileSystemManager, EntityManagerInterface $entityManager): Response
{
Expand All @@ -196,9 +195,7 @@ public function relocate(Book $book, BookFileSystemManager $fileSystemManager, E

return $this->redirectToRoute('app_book', [
'book' => $book->getId(),
'slug'=> $book->getSlug(),
'slug' => $book->getSlug(),
]);
}


}
4 changes: 2 additions & 2 deletions src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public function getCoverDirectory(): string
/**
* @return \Iterator<\SplFileInfo>
*/
public function getAllBooksFiles(bool $onlyConsumeDirectory=false): \Iterator
public function getAllBooksFiles(bool $onlyConsumeDirectory = false): \Iterator
{
try {
$finder = new Finder();
$finder->files()->name(self::ALLOWED_FILE_EXTENSIONS);
if($onlyConsumeDirectory) {
if ($onlyConsumeDirectory) {
$finder->in($this->getBooksDirectory().'/consume');
} else {
$finder->in($this->getBooksDirectory());
Expand Down
22 changes: 7 additions & 15 deletions src/Service/BookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@
use Kiwilan\Ebook\EbookCover;
use Kiwilan\Ebook\Tools\BookAuthor;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* @phpstan-type MetadataType array{ title:string, authors: BookAuthor[], main_author: ?BookAuthor, description: ?string, publisher: ?string, publish_date: ?\DateTime, language: ?string, tags: string[], serie:?string, serie_index: ?int, cover: ?EbookCover }
*/
class BookManager
{

public function __construct(private KernelInterface $appKernel, private BookFileSystemManager $fileSystemManager, private EntityManagerInterface $entityManager, private BookRepository $bookRepository)
public function __construct(private BookFileSystemManager $fileSystemManager, private EntityManagerInterface $entityManager, private BookRepository $bookRepository)
{
}

Expand Down Expand Up @@ -137,12 +134,12 @@ public function extractEbookMetadata(\SplFileInfo $file): array
return $data;
}

public function consumeBooks(array $files, ?InputInterface $input=null, ?OutputInterface $output=null):void
public function consumeBooks(array $files, InputInterface $input = null, OutputInterface $output = null): void
{
if($output===null) {
if ($output === null) {
$output = new NullOutput();
}
if($input===null) {
if ($input === null) {
$input = new StringInput('');
}
$io = new SymfonyStyle($input, $output);
Expand All @@ -157,16 +154,11 @@ public function consumeBooks(array $files, ?InputInterface $input=null, ?OutputI
$book = $this->consumeBook($file);
$progressBar->setMessage($file->getFilename());

if ($book===null) {
continue;
}

$this->entityManager->persist($book);
$this->entityManager->flush();

} catch (\Exception $e) {
$output->error('died during process of '.$file->getRealPath());
$output->error($e->getMessage());
$io->error('died during process of '.$file->getRealPath());
$io->error($e->getMessage());
throw $e;
}
$book = null;
Expand All @@ -175,7 +167,7 @@ public function consumeBooks(array $files, ?InputInterface $input=null, ?OutputI
$progressBar->finish();
}

public function consumeBook(\SplFileInfo $file):Book
public function consumeBook(\SplFileInfo $file): Book
{
$book = $this->bookRepository->findOneBy(
[
Expand Down

0 comments on commit 2f5d00b

Please sign in to comment.