Commit 7d619b6d authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Ajout de la ligne de commande de suppression/purge de rendus .

parent e8c522bf
Loading
Loading
Loading
Loading
Loading
+4 −0
Changes for CHANGELOG.md: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
CHANGELOG
=========

7.3.0
----
- Ajout de la ligne de commande de suppression/purge de rendus `renderer:purger-rendu [-t|--type TYPE] [--dry-run] <interval>`.

7.2.6
----
- Forçage de la sélection de "1M" sur l'index des rendus pour éviter des crashs mémoire
+20 −0
Changes for README.md: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -79,6 +79,26 @@ _Remarque :_
A faire en fin de fichier une seul fois, le JS s'occupe de l'associer aux différents input


Lignes de commandes fournies
----------------------------

- Suppression des rendus selon leur ancienneté (date de génération) : `renderer:purger-rendu [options] <interval>`

```
Usage:
renderer:purger-rendu [options] <interval>

Arguments:
interval            Intervalle de temps spécifiant l'ancienneté maximum du rendu, au format PHP. 
                    Exemples : P1Y (1 an), P6M (6 mois). 
                    Les rendus plus anciens que cet intervalle seront supprimés.

Options:
-t, --type=TYPE     Type de template dont est issu le rendu (ex : mail, pdf, texte).
--dry-run           Lancement pour de faux !
```


Administration
--------------

+18 −0
Changes for config/merged/command.config.php: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

use UnicaenRenderer\Command\RenduPurgeCommand;
use UnicaenRenderer\Command\RenduPurgeCommandFactory;

return [
    'laminas-cli' => [
        'commands' => [
            RenduPurgeCommand::NAME => RenduPurgeCommand::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            RenduPurgeCommand::class => RenduPurgeCommandFactory::class,
        ],
    ],
];
+119 −0
Changes for src/UnicaenRenderer/Command/RenduPurgeCommand.php: 119 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace UnicaenRenderer\Command;

use DateInterval;
use DateTime;
use Doctrine\ORM\Query\Expr\Join;
use Exception;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
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 UnicaenRenderer\Service\Rendu\RenduServiceAwareTrait;

class RenduPurgeCommand extends Command
{
    use LoggerAwareTrait;
    use RenduServiceAwareTrait;

    public const NAME = 'renderer:purger-rendu';

    private string $interval;
    private ?string $type = null;
    private bool $dryRun = false;

    protected function configure(): void
    {
        parent::configure();

        $this->setDescription("Suppression des rendus selon leur ancienneté");
        $this->addArgument(
            'interval',
            InputArgument::REQUIRED,
            "Intervalle de temps spécifiant l'ancienneté maximum du rendu, au format PHP. " .
            "Exemples : P1Y (1 an), P6M (6 mois). " .
            "Les rendus plus anciens que cet intervalle seront supprimés.",
        );
        $this->addOption(
            'type',
            't',
            InputOption::VALUE_REQUIRED,
            "Type de template dont est issu le rendu (ex : mail, pdf, texte)."
        );
        $this->addOption(
            'dry-run',
            null,
            InputOption::VALUE_NONE,
            "Lancement pour de faux !"
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->interval = $input->getArgument('interval');
        $this->type = $input->getOption('type');
        $this->dryRun = (bool) $input->getOption('dry-run');

        $io = new SymfonyStyle($input, $output);
        try {
            $dateMax = new DateTime();
            $dateMax->sub(new DateInterval($this->interval));
            $dateMax->setTime(0, 0);

            $io->title(sprintf(
                "Suppression de rendus%s",
                $this->dryRun ? " POUR DE FAUX" : ''
            ));

            $renduIds = $this->fetchRenduIds($dateMax);

            $io->text(sprintf(
                "Nombre de rendus datant d'avant le %s : %d",
                $dateMax->format("d/m/Y à H:i"),
                count($renduIds)
            ));

            if ($renduIds) {
                if (!$this->dryRun) {
                    $this->renduService->deleteById($renduIds);
                    $io->success(sprintf("Nombre de rendus supprimés : %d", count($renduIds)));
                } else {
                    $io->success(sprintf("Nombre de rendus qui auraient été supprimés : %d", count($renduIds)));
                }
            } else {
                $io->text("Aucun rendu à supprimer.");
            }

            $io->text("Terminé.");
        } catch (Exception $e) {
            $io->error($e->getMessage());
            return self::FAILURE;
        }

        return self::SUCCESS;
    }

    /**
     * @return int[]
     */
    private function fetchRenduIds(DateTime $dateMax): array
    {
        $qb = $this->renduService->createQueryBuilder()
            ->select('contenu.id')
            ->andWhere("contenu.date < :dateMax")
            ->orderBy("contenu.id")
            ->setParameter("dateMax", $dateMax);

        if ($this->type) {
            $qb
                ->join('contenu.template', 't', Join::WITH, 't.type = :type')
                ->setParameter('type', $this->type);
        }

        return array_map('current', $qb->getQuery()->getScalarResult());
    }
}
 No newline at end of file
+27 −0
Changes for src/UnicaenRenderer/Command/RenduPurgeCommandFactory.php: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace UnicaenRenderer\Command;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use UnicaenRenderer\Service\Rendu\RenduService;

class RenduPurgeCommandFactory implements FactoryInterface
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): RenduPurgeCommand
    {
        $command = new RenduPurgeCommand();

        /** @var \UnicaenRenderer\Service\Rendu\RenduService $renduService */
        $renduService = $container->get(RenduService::class);
        $command->setRenduService($renduService);

        return $command;
    }
}
 No newline at end of file
Loading