62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Entity\Upload;
|
|
use App\Service\Pdf\Sanitizer;
|
|
use App\Service\Upload\UploadHandler;
|
|
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\Output\OutputInterface;
|
|
|
|
#[AsCommand(name: 'app:uploads:convert', description: 'Converts all uploads to sanitized PDFs')]
|
|
class ConvertUploadsCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly Sanitizer $sanitizer,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$uploads = $this
|
|
->entityManager
|
|
->getRepository(Upload::class)
|
|
->findBy([
|
|
'type' => [Upload::TYPE_INVOICE, Upload::TYPE_CONTRACT],
|
|
]);
|
|
|
|
$progressBar = new ProgressBar($output, count($uploads));
|
|
$progressBar->start();
|
|
|
|
foreach ($uploads as $upload) {
|
|
/** @var Upload $upload */
|
|
$filepath = $this->uploadHandler->getUploadFilepath($upload);
|
|
try {
|
|
$targetFilepath = $this->sanitizer->sanitizeFile($filepath);
|
|
$upload
|
|
->setFilename(basename($targetFilepath))
|
|
->setMimeType('application/pdf')
|
|
;
|
|
} catch (\Throwable $e) {
|
|
$output->writeln('<error>'.$e->getMessage().'</error>');
|
|
}
|
|
|
|
$progressBar->advance();
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
$progressBar->finish();
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|