2020-04-12 17:28:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Entity\Owner;
|
2020-04-12 17:35:57 +02:00
|
|
|
use App\Entity\Poll;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Swift_Message;
|
2020-04-14 18:09:03 +02:00
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
|
use Symfony\Component\Mime\Address;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
class MailService {
|
|
|
|
|
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
/**
|
|
|
|
* @var \Swift_Mailer
|
|
|
|
*/
|
|
|
|
private $mailer;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $mailer) {
|
2020-04-12 17:35:57 +02:00
|
|
|
$this->em = $entityManager;
|
2020-04-14 18:09:03 +02:00
|
|
|
$this->mailer = $mailer;
|
2020-04-12 17:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, \Swift_Mailer $mailer ) {
|
|
|
|
$em = $this->em->getRepository( Owner::class );
|
|
|
|
$admin_user = $foundOwner;
|
|
|
|
$poll = $newpoll;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
// anti spam , limit to every minute
|
|
|
|
$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 = [
|
|
|
|
'owner' => $admin_user,
|
|
|
|
'poll' => $poll,
|
|
|
|
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
|
|
|
$message = ( new Swift_Message( 'Framadate - mes sondages' ) )
|
|
|
|
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
2020-04-13 17:35:57 +02:00
|
|
|
->setContentType('text/html')
|
|
|
|
->setCharset('UTF-8')
|
2020-04-12 17:28:30 +02:00
|
|
|
->setTo( $admin_user->getEmail() )
|
2020-04-14 18:09:03 +02:00
|
|
|
->htmlTemplate($templateVars[ 'email_template' ])
|
|
|
|
->context( $templateVars);
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
// send email
|
|
|
|
return $mailer->send( $message );
|
|
|
|
|
|
|
|
}
|
2020-04-14 18:09:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* send created polls to an owner
|
|
|
|
* @param Owner $foundOwner
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function sendOwnerPollsAction( Owner $foundOwner ) {
|
|
|
|
$em = $this->em->getRepository( Owner::class );
|
|
|
|
$admin_user = $foundOwner;
|
|
|
|
|
|
|
|
|
|
|
|
// anti spam , limit to every minute
|
|
|
|
$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();
|
|
|
|
$titleEmail = 'Framadate | Mes sondages';
|
|
|
|
|
|
|
|
$templateVars = [
|
|
|
|
'owner' => $admin_user,
|
|
|
|
'title' => $titleEmail,
|
|
|
|
'email_template' => 'emails/owner-polls.html.twig',
|
|
|
|
];
|
|
|
|
|
2020-04-14 18:13:48 +02:00
|
|
|
$email = ( new TemplatedEmail( ) )
|
2020-04-14 18:09:03 +02:00
|
|
|
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
|
|
|
->to( new Address($admin_user->getEmail() ) )
|
2020-04-14 18:13:48 +02:00
|
|
|
->subject($titleEmail)
|
2020-04-14 18:09:03 +02:00
|
|
|
->htmlTemplate($templateVars[ 'email_template' ])
|
|
|
|
->context( $templateVars);
|
|
|
|
|
|
|
|
// send email
|
|
|
|
return $this->mailer->send( $email );
|
|
|
|
}
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|