Commit fb07ea87 authored by Mat's avatar Mat

La commande qui archive les offres expirées enlève aussi tous les tokens périmés

parent aee4fd51
......@@ -4,12 +4,29 @@ namespace PotageBundle\Command;
use PotageBundle\Entity\Offre;
use PotageBundle\Entity\User;
use PotageBundle\Services\LettreToken;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CronOffreCommand extends ContainerAwareCommand
{
private $lettreToken;
/**
* SendMailCommand constructor.
*
* @param LettreToken $lettreToken
*/
public function __construct(LettreToken $lettreToken)
{
parent::__construct();
$this->lettreToken = $lettreToken;
}
/**
* {@inheritdoc}
*/
......@@ -32,7 +49,9 @@ class CronOffreCommand extends ContainerAwareCommand
{
$em = $this->getContainer()->get('doctrine')->getManager();
$currentOffre = $em->getRepository('PotageBundle:Offre')->findAllByStatusForRead('current');
$currentOffre = $em->getRepository('PotageBundle:Offre')
->findAllByStatusForRead('current');
$now = new \DateTime('now');
......@@ -43,13 +62,69 @@ class CronOffreCommand extends ContainerAwareCommand
{
if ($offre->getEndedAt() <= $now) {
$output->writeln($offre->getReference() . ': offre expirée, on archive..');
$output->write(
"\n"
. $offre->getReference()
. ": offre expired since "
. date_format($offre->getEndedAt(), "d/m/Y H:i")
. ", change status to archive..\n");
$offre->setStatus('closed');
$ret = $this->revoquePotageUsersTokens($offre);
$output->writeln($ret);
// TODO envoyer un mail au gestionnaire pour notifier l'expiration de l'offre
}
}
$em->flush();
$output->write("\nDone\n");
}
/**
* On parcourt la table User, colonne tokenKey
* Chaque token est décodé et s'il correspond à l'offre expirée, on le supprime
*
* @param Offre $offre
* @return array
*/
private function revoquePotageUsersTokens(Offre $offre)
{
$em = $this->getContainer()
->get('doctrine')->getManager();
$usersTokenKeys = $em->getRepository('PotageBundle:User')
->findAllTokenKeys();
$log = array();
foreach ($usersTokenKeys as $userTokenKeys)
{
foreach ($userTokenKeys['tokenKey'] as $uTokenKey)
{
$uOffre = intval($this->lettreToken->decodePotageUserToken($uTokenKey)['offre_id']);
if ($uOffre === $offre->getId())
{
/**
* @var User $user
*/
$user = $em->getRepository('PotageBundle:User')
->find($userTokenKeys['id']);
$user->removeTokenKey($uTokenKey);
$log[] = " User ". $userTokenKeys['id'] . ", removing token ". $uTokenKey;
}
}
}
$em->flush();
count($log) === 0 ? $log[] = " No tokens detected for this offre !" : null ;
return $log;
}
}
......@@ -129,4 +129,15 @@ class User extends BaseUser
return $this;
}
/**
* @param $tokenKey
* @return User
*/
public function removeTokenKey($tokenKey)
{
if (($key = array_search($tokenKey, $this->tokenKey)) !== false) {
unset($this->tokenKey[$key]);
}
return $this;
}
}
......@@ -43,4 +43,18 @@ class UserRepository extends \Doctrine\ORM\EntityRepository implements UserLoade
->getOneOrNullResult();
}
/**
* Retourne la colonne des token_keys de tous les users
* @return array
*/
public function findAllTokenKeys()
{
return $this->createQueryBuilder('u')
->select('u.id', 'u.tokenKey')
->getQuery()
->getResult();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment