Skip to content
Snippets Groups Projects
Commit 5df5ad17 authored by Thibaut Vallee's avatar Thibaut Vallee
Browse files

Mise en place de la commande de purge

parent 68e67bfb
No related branches found
No related tags found
No related merge requests found
Pipeline #35460 passed
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
......
......@@ -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,
],
],
);
......@@ -52,4 +52,6 @@ return [
// ],
// ]
// ],
// 'conservation-time' => new DateInterval('P2Y'),
];
......@@ -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'),
],
];
```
......
<?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
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment