Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added command to easy replace cover #132

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@
<code>isset($this-&gt;newIndexName)</code>
</RedundantPropertyInitializationCheck>
</file>
<file src="src/Service/VendorService/OverDrive/OverDriveBooksVendorService.php">
<PossiblyNullArgument occurrences="3">
<code>$clientId</code>
<code>$clientSecret</code>
<code>$libraryAccountEndpoint</code>
</PossiblyNullArgument>
</file>
<file src="src/Service/VendorService/VendorServiceTrait.php">
<UndefinedConstant occurrences="1">
<code>$this::VENDOR_ID</code>
Expand Down
97 changes: 97 additions & 0 deletions src/Command/Source/SourceReplaceCoversCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* @file
* Helper command to search the source table for potential new images.
*/

namespace App\Command\Source;

use App\Entity\Source;
use App\Entity\Vendor;
use App\Message\VendorImageMessage;
use App\Repository\SourceRepository;
use App\Repository\VendorRepository;
use App\Utils\Types\VendorState;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;

/**
* Class SourceReplaceCoversCommand.
*/
#[AsCommand(name: 'app:source:replace')]
class SourceReplaceCoversCommand extends Command
{
/**
* SourceDownloadCoversCommand constructor.
*
* @param EntityManagerInterface $em
* The entity manager to access that database
* @param MessageBusInterface $bus
* Message bus to send messages (jobs)
* @param SourceRepository $sourceRepository
* Source repository class to access source entities
* @param VendorRepository $vendorRepository
* Vendor repository class to access vendor entities
*/
public function __construct(
private readonly EntityManagerInterface $em,
private readonly MessageBusInterface $bus,
private readonly SourceRepository $sourceRepository,
private readonly VendorRepository $vendorRepository,
) {
parent::__construct();
}

/**
* Define the command.
*/
protected function configure(): void
{
$this->setDescription('Replace cover for a given identifier')
->addOption('vendor-id', null, InputOption::VALUE_REQUIRED, 'Vendor to replace cover for')
->addOption('identifier', null, InputOption::VALUE_REQUIRED, 'Identifier to replace cover for')
->addOption('url', null, InputOption::VALUE_REQUIRED, 'URL to the new cover to use in replacement');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$vendorId = $input->getOption('vendor-id');
$identifier = $input->getOption('identifier');
$url = $input->getOption('url');

if (null === $vendorId || null === $identifier || null === $url) {
$output->writeln('<error>Required parameters was not given</error>');
}

/** @var Vendor $vendor */
$vendor = $this->vendorRepository->findOneBy(['id' => $vendorId]);
/** @var Source $source */
$source = $this->sourceRepository->findOneBy(['matchId' => $identifier, 'vendor' => $vendor]);

// Replace image in source entity.
$source->setOriginalFile($url);
$source->setLastIndexed(new \DateTime('now -1 day'));
$source->setOriginalContentLength(null);
$source->setOriginalLastModified(null);
$this->em->flush();

// Trigger download, validate and reindex with the new cover.
$message = new VendorImageMessage();
$message->setOperation(VendorState::UPDATE)
->setIdentifier($source->getMatchId())
->setVendorId($source->getVendor()->getId())
->setIdentifierType($source->getMatchType());
$this->bus->dispatch($message);

return Command::SUCCESS;
}
}