1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

remove date diff

This commit is contained in:
tykayn 2020-04-14 18:16:09 +02:00
parent 81b27811c2
commit 00fe90b66b

View File

@ -6,7 +6,6 @@ namespace App\Service;
use App\Entity\Owner; use App\Entity\Owner;
use App\Entity\Poll; use App\Entity\Poll;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Swift_Message; use Swift_Message;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
@ -24,8 +23,8 @@ class MailService {
*/ */
private $mailer; private $mailer;
public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $mailer) { public function __construct( EntityManagerInterface $entityManager, \Swift_Mailer $mailer ) {
$this->em = $entityManager; $this->em = $entityManager;
$this->mailer = $mailer; $this->mailer = $mailer;
} }
@ -35,17 +34,7 @@ class MailService {
$poll = $newpoll; $poll = $newpoll;
// anti spam , limit to every minute // anti spam , limit to every minute TODO
$lastSend = $admin_user->getRequestedPollsDate();
$now = new \DateTime();
if ( date_diff( $lastSend, $now ) < 60 ) {
// too soon!
die( 'too soon!' );
}
$admin_user->setRequestedPollsDate( $now );
$em->persist( $admin_user );
$em->flush();
$templateVars = [ $templateVars = [
'owner' => $admin_user, 'owner' => $admin_user,
@ -56,11 +45,11 @@ class MailService {
$message = ( new Swift_Message( 'Framadate - mes sondages' ) ) $message = ( new Swift_Message( 'Framadate - mes sondages' ) )
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
->setContentType('text/html') ->setContentType( 'text/html' )
->setCharset('UTF-8') ->setCharset( 'UTF-8' )
->setTo( $admin_user->getEmail() ) ->setTo( $admin_user->getEmail() )
->htmlTemplate($templateVars[ 'email_template' ]) ->htmlTemplate( $templateVars[ 'email_template' ] )
->context( $templateVars); ->context( $templateVars );
// send email // send email
return $mailer->send( $message ); return $mailer->send( $message );
@ -69,6 +58,7 @@ class MailService {
/** /**
* send created polls to an owner * send created polls to an owner
*
* @param Owner $foundOwner * @param Owner $foundOwner
* *
* @return int * @return int
@ -79,17 +69,17 @@ class MailService {
$admin_user = $foundOwner; $admin_user = $foundOwner;
// anti spam , limit to every minute // anti spam , limit to every minute TODO
$lastSend = $admin_user->getRequestedPollsDate(); // $lastSend = $admin_user->getRequestedPollsDate();
$now = new \DateTime(); // $now = new \DateTime();
if ( date_diff( $lastSend, $now ) < 60 ) { // if ( date_diff( $lastSend, $now ) < 60 ) {
// too soon! // // too soon!
die( 'too soon!' ); // die( 'too soon!' );
} // }
$admin_user->setRequestedPollsDate( $now ); // $admin_user->setRequestedPollsDate( $now );
$em->persist( $admin_user ); // $em->persist( $admin_user );
$em->flush(); // $em->flush();
$titleEmail = 'Framadate | Mes sondages'; $titleEmail = 'Framadate | Mes sondages';
$templateVars = [ $templateVars = [
@ -98,12 +88,12 @@ class MailService {
'email_template' => 'emails/owner-polls.html.twig', 'email_template' => 'emails/owner-polls.html.twig',
]; ];
$email = ( new TemplatedEmail( ) ) $email = ( new TemplatedEmail() )
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) ->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
->to( new Address($admin_user->getEmail() ) ) ->to( new Address( $admin_user->getEmail() ) )
->subject($titleEmail) ->subject( $titleEmail )
->htmlTemplate($templateVars[ 'email_template' ]) ->htmlTemplate( $templateVars[ 'email_template' ] )
->context( $templateVars); ->context( $templateVars );
// send email // send email
return $this->mailer->send( $email ); return $this->mailer->send( $email );