-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45f2906
commit c103e9d
Showing
6 changed files
with
251 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Command; | ||
|
||
use App\Entity\Book; | ||
use App\Repository\BookRepository; | ||
use App\Service\BookFileSystemManager; | ||
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; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
#[AsCommand( | ||
name: 'books:extract-cover', | ||
description: 'Add a short description for your command', | ||
)] | ||
class BooksExtractCoverCommand extends Command | ||
{ | ||
public function __construct(private BookFileSystemManager $fileSystemManager, private BookRepository $bookRepository, private EntityManagerInterface $entityManager) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the extraction of the cover') | ||
->addArgument('book-id', InputOption::VALUE_REQUIRED, 'Which book to extract the cover from, use "all" for all books') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$bookId = $input->getArgument('book-id'); | ||
$force = $input->getOption('force'); | ||
if ($bookId === 'all') { | ||
$books = $this->bookRepository->findAll(); | ||
} else { | ||
$books = $this->bookRepository->findBy(['id' => $bookId]); | ||
} | ||
|
||
if (count($books) === 0) { | ||
$io->error('No books found'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$io->note(sprintf('Processing: %s book(s)', count($books))); | ||
|
||
$progressBar = new ProgressBar($output, count($books)); | ||
$progressBar->start(); | ||
$fs = new Filesystem(); | ||
foreach ($books as $book) { | ||
/* @var Book $book */ | ||
$progressBar->advance(); | ||
|
||
if ($force === true || $book->getImageFilename() === null || !$fs->exists($book->getImagePath().$book->getImageFilename())) { | ||
try { | ||
$book = $this->fileSystemManager->extractCover($book); | ||
$this->entityManager->persist($book); | ||
} catch (\Exception $e) { | ||
$io->error($e->getMessage()); | ||
continue; | ||
} | ||
} | ||
} | ||
$this->entityManager->flush(); | ||
|
||
$progressBar->finish(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters