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\EntityManagerInterface;
|
2020-04-14 18:39:29 +02:00
|
|
|
use Exception;
|
|
|
|
use Swift_Mailer;
|
2020-04-12 17:35:57 +02:00
|
|
|
use Swift_Message;
|
2020-04-14 18:09:03 +02:00
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
|
use Symfony\Component\Mime\Address;
|
2020-04-14 18:39:29 +02:00
|
|
|
use Symfony\Component\Mime\Email;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
class MailService {
|
|
|
|
|
|
|
|
|
2020-04-14 18:09:03 +02:00
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
/**
|
2020-04-14 18:39:29 +02:00
|
|
|
* @var Swift_Mailer
|
2020-04-14 18:09:03 +02:00
|
|
|
*/
|
|
|
|
private $mailer;
|
|
|
|
|
2020-04-14 18:39:29 +02:00
|
|
|
public function __construct( EntityManagerInterface $entityManager, Swift_Mailer $mailer ) {
|
2020-04-14 18:16:09 +02:00
|
|
|
$this->em = $entityManager;
|
2020-04-14 18:09:03 +02:00
|
|
|
$this->mailer = $mailer;
|
2020-04-12 17:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 18:39:29 +02:00
|
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $newpoll, Swift_Mailer $mailer ) {
|
2020-04-12 17:35:57 +02:00
|
|
|
$em = $this->em->getRepository( Owner::class );
|
|
|
|
$admin_user = $foundOwner;
|
|
|
|
$poll = $newpoll;
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// anti spam , limit to every minute TODO
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
$templateVars = [
|
|
|
|
'owner' => $admin_user,
|
|
|
|
'poll' => $poll,
|
|
|
|
'title' => 'Création de sondage - ' . $poll->getTitle(),
|
|
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
|
|
];
|
|
|
|
|
2020-04-14 18:39:29 +02:00
|
|
|
$email = ( new Email( ) )
|
2020-04-12 17:28:30 +02:00
|
|
|
->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
2020-04-14 18:16:09 +02:00
|
|
|
->setContentType( 'text/html' )
|
|
|
|
->setCharset( 'UTF-8' )
|
2020-04-14 18:39:29 +02:00
|
|
|
->subject('Framadate - mes sondages')
|
2020-04-12 17:28:30 +02:00
|
|
|
->setTo( $admin_user->getEmail() )
|
2020-04-14 18:16:09 +02:00
|
|
|
->htmlTemplate( $templateVars[ 'email_template' ] )
|
|
|
|
->context( $templateVars );
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
// send email
|
2020-04-14 18:39:29 +02:00
|
|
|
return $mailer->send( $email );
|
2020-04-12 17:28:30 +02:00
|
|
|
|
|
|
|
}
|
2020-04-14 18:09:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* send created polls to an owner
|
2020-04-14 18:16:09 +02:00
|
|
|
*
|
2020-04-14 18:09:03 +02:00
|
|
|
* @param Owner $foundOwner
|
|
|
|
*
|
|
|
|
* @return int
|
2020-04-14 18:39:29 +02:00
|
|
|
* @throws Exception
|
2020-04-14 18:09:03 +02:00
|
|
|
*/
|
|
|
|
public function sendOwnerPollsAction( Owner $foundOwner ) {
|
|
|
|
$em = $this->em->getRepository( Owner::class );
|
|
|
|
$admin_user = $foundOwner;
|
|
|
|
|
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// anti spam , limit to every minute TODO
|
|
|
|
// $lastSend = $admin_user->getRequestedPollsDate();
|
|
|
|
// $now = new \DateTime();
|
2020-04-14 18:09:03 +02:00
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
// if ( date_diff( $lastSend, $now ) < 60 ) {
|
|
|
|
// // too soon!
|
|
|
|
// die( 'too soon!' );
|
|
|
|
// }
|
|
|
|
// $admin_user->setRequestedPollsDate( $now );
|
|
|
|
// $em->persist( $admin_user );
|
|
|
|
// $em->flush();
|
2020-04-14 18:09:03 +02:00
|
|
|
$titleEmail = 'Framadate | Mes sondages';
|
|
|
|
|
|
|
|
$templateVars = [
|
|
|
|
'owner' => $admin_user,
|
|
|
|
'title' => $titleEmail,
|
2020-04-14 18:39:29 +02:00
|
|
|
'email_template' => 'emails/owner-list.html.twig',
|
2020-04-14 18:09:03 +02:00
|
|
|
];
|
|
|
|
|
2020-04-14 18:16:09 +02:00
|
|
|
$email = ( new TemplatedEmail() )
|
2020-04-14 18:09:03 +02:00
|
|
|
->from( 'ne-pas-repondre@framadate-api.cipherbliss.com' )
|
2020-04-14 18:16:09 +02:00
|
|
|
->to( new Address( $admin_user->getEmail() ) )
|
|
|
|
->subject( $titleEmail )
|
|
|
|
->htmlTemplate( $templateVars[ 'email_template' ] )
|
|
|
|
->context( $templateVars );
|
2020-04-14 18:09:03 +02:00
|
|
|
|
|
|
|
// send email
|
|
|
|
return $this->mailer->send( $email );
|
|
|
|
}
|
2020-04-12 17:28:30 +02:00
|
|
|
}
|