-
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
67f7757
commit e24dd50
Showing
1 changed file
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace App\Command; | ||
|
||
use App\Entity\Book; | ||
use App\Repository\BookRepository; | ||
use App\Service\BookSuggestions; | ||
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:tag', | ||
description: 'Add a short description for your command', | ||
)] | ||
class BooksTagCommand extends Command | ||
{ | ||
public function __construct(private BookSuggestions $bookSuggestions, private BookRepository $bookRepository, private EntityManagerInterface $entityManager) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->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'); | ||
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 ($book->getTags() !== null && count($book->getTags()) > 0) { | ||
continue; | ||
} | ||
|
||
$suggestions = $this->bookSuggestions->getCategorySuggestions($book); | ||
if (count($suggestions['tags']) === 0) { | ||
$suggestions = $this->bookSuggestions->getGoogleSuggestions($book); | ||
} | ||
$book->setTags($suggestions['tags']); | ||
|
||
$summary = count($suggestions['summary']) > 0 ? current($suggestions['summary']) : ''; | ||
if ($summary !== '' && ($book->getSummary() === null || $book->getSummary() === '')) { | ||
$book->setSummary($summary); | ||
} | ||
} | ||
$this->entityManager->flush(); | ||
|
||
$progressBar->finish(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |