diff --git a/CHANGELOG b/CHANGELOG index 970881e9f434d42f45d3f12b28c446fb9df3df05..8c77c449a1b6c1f24805a8faa20f1261044cdfd2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +7.1.3 +----- +- Mise en place de la commande mail:purge pour supprimer les mails envoyée avant une date + 7.1.2 ----- - Correction pour caster la variable redirectTo en Array diff --git a/config/merged/mail.config.php b/config/merged/mail.config.php index a9d0c0da2ef0b878b482b0010630708c1494b0c2..5e8d82906ead3f352e1077ff3bd81ce57a99674a 100644 --- a/config/merged/mail.config.php +++ b/config/merged/mail.config.php @@ -10,6 +10,8 @@ use UnicaenMail\View\Helper\MailViewHelper; use UnicaenPrivilege\Guard\PrivilegeController; use Laminas\Router\Http\Literal; use Laminas\Router\Http\Segment; +use UnicaenMail\Command\MailPurgeCommand; +use UnicaenMail\Command\MailPurgeCommandFactory; return array( 'bjyauthorize' => [ @@ -134,6 +136,7 @@ return array( 'service_manager' => [ 'factories' => [ MailService::class => MailServiceFactory::class, + MailPurgeCommand::class => MailPurgeCommandFactory::class, ], ], @@ -148,4 +151,10 @@ return array( 'mails' => MailsViewHelper::class, ], ], + + 'laminas-cli' => [ + 'commands' => [ + 'mail:purge' => MailPurgeCommand::class, + ], + ], ); diff --git a/config/unicaen-mail.local.php.dist b/config/unicaen-mail.local.php.dist index 454fa6d44f2d8383cb83f668df7d9bca07748472..f972aabc11c2e0e2e4c68216405a32eaa5e71249 100644 --- a/config/unicaen-mail.local.php.dist +++ b/config/unicaen-mail.local.php.dist @@ -52,4 +52,6 @@ return [ // ], // ] // ], + +// 'conservation-time' => new DateInterval('P2Y'), ]; diff --git a/readme.md b/readme.md index d14ce396c40483876b448676d197d73175e8c52d..fcdc7c10c58776759032adfdf1549e464bb99d21 100644 --- a/readme.md +++ b/readme.md @@ -60,6 +60,20 @@ $options['droits'][ ] ``` +Purge des mails +--- +La commande console `mail:purge` permet de supprimer les mails envoyées avant une date. + +- La durée par défaut de conservertion est de 1 an, il s'agit d'un paramétre modifiable dans la configuration sous la forme de DateInterval : +``` + 'unicaen-mail' => [ + 'conservation-time' => new DateInterval('P2Y'), + ], +``` +1 option possibles : +- `--time=P2Y` *(ou `-t P2Y`)* pour spécifier la durée de conservations des mails (on ignore alors celle définie en conf). + + Configuration ============= @@ -98,6 +112,11 @@ return [ 'subject_prefix' => '[Mon App]', 'from_name' => 'Mon application', 'from_email' => 'ne-pas-repondre@mail.fr' + + /** + * Durée de conservertions des mails + */ + 'conservation-time' => new DateInterval('P2Y'), ], ]; ``` diff --git a/src/UnicaenMail/Command/MailPurgeCommand.php b/src/UnicaenMail/Command/MailPurgeCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..b62e6472fdaa91857eb30540398c586c05326e8a --- /dev/null +++ b/src/UnicaenMail/Command/MailPurgeCommand.php @@ -0,0 +1,103 @@ +<?php + +namespace UnicaenMail\Command; + +use DateInterval; +use DateTime; +use Exception; +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\Console\Style\SymfonyStyle; +use UnicaenEtat\Entity\Db\EtatInstance; +use UnicaenEtat\Service\EtatInstance\EtatInstanceServiceAwareTrait; +use UnicaenMail\Entity\Db\Mail; +use UnicaenMail\Service\Mail\MailServiceAwareTrait; + +/*** + * @desc Supprime toutes les instances d'état qui sont archivées depuis un certains temps + * paramétre à définir dans config unicaen-etat > conservation-time sour la forme d'un DateInterval + * Par défaut : 1 an + * TODO : permettre dans la conf de spécifier les types et catégories d'états qui doivent être purgée + * TODO : gérer des dates différents par état/types + * TODO : rendre des types/catégories non purgeable + */ +class MailPurgeCommand extends Command +{ + use MailServiceAwareTrait; + + protected static $defaultName = 'mail:purge'; + + + protected ?DateInterval $conservationTime = null; + public function setConservationTime(DateInterval $conservationTime): static + { + $this->conservationTime = $conservationTime; + return $this; + } + public function getDateSuppression() : DateTime + { + $date = new DateTime(); + $conservationTime = ($this->conservationTime) ?? new DateInterval('P1Y'); + $date->sub($conservationTime); + return $date; + } + + protected function configure() : static + { + $this->setDescription("Supprime les instances de mails qui ont été envoyée depuis 'longtemps'"); + $this->addOption('time', '-t', InputOption::VALUE_OPTIONAL, "Intervale de temps que l'on souhaite conserver -- format : P1Y"); + return $this; + } + + protected function initialize(InputInterface $input, OutputInterface $output): void + { + $io = new SymfonyStyle($input, $output); + try{ + $time = $input->getOption('time'); + if(isset($time) && $time != ""){ + $this->conservationTime = new DateInterval($time); + } + } + catch (Exception $e){ + $io->error($e->getMessage()); + exit(-1); + } + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + try { + $io->title("Suppression des instances de mails"); + + $date = $this->getDateSuppression(); + $qb =$this->getMailService()->createQueryBuilder(); + $qb->andWhere("mail.dateEnvoi < :date"); + $qb->setParameter("date", $date); + $mails = $qb->getQuery()->getResult(); + + if(!empty($mails)) { + $io->progressStart(sizeof($mails)); + /** @var Mail $mail */ + foreach ($mails as $mail) { + $this->getMailService()->delete($mail); + $io->progressAdvance(); + } + $io->progressFinish(); + } + else{ + $io->text("Aucun mail à supprimer"); + } + $io->success("Suppression terminée"); + } + catch (Exception $e){ + $io->error($e->getMessage()); + return self::FAILURE; + } + return self::SUCCESS; + } + +} \ No newline at end of file diff --git a/src/UnicaenMail/Command/MailPurgeCommandFactory.php b/src/UnicaenMail/Command/MailPurgeCommandFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..69b054bb051c272c67783f65cc671c755099b750 --- /dev/null +++ b/src/UnicaenMail/Command/MailPurgeCommandFactory.php @@ -0,0 +1,35 @@ +<?php + +namespace UnicaenMail\Command; + +use DateInterval; +use Exception; +use Mail\Service\Mail\MailService; +use Psr\Container\ContainerInterface; + +class MailPurgeCommandFactory +{ + /** + * @param ContainerInterface $container + * + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \Exception + */ + public function __invoke(ContainerInterface $container): MailPurgeCommand + { + $command = new MailPurgeCommand(); + $command->setMailService($container->get(MailService::class)); + + $config = $container->get('Configuration'); + if (isset($config['unicaen-mail']['conservation-time'])) { + $conservationTime = $config['unicaen-mail']['conservation-time']; + if(!$conservationTime instanceof DateInterval) { + throw new Exception("Le paramètre de coniguration 'unicaen-mail > conservation-time' doit être un DateInterval"); + } + $command->setConservationTime($conservationTime); + } + + return $command; + } +} \ No newline at end of file