<?php declare( strict_types=1 );
namespace App\EventListener;
use App\Event\ReviewEvent;
use App\Manager\CrmManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class ReviewSubscriber implements EventSubscriberInterface
{
private $attachment_dir;
private $mailer;
private $logger;
private $template;
private $crmManager;
public function __construct(
$attachment_dir,
\Swift_Mailer $mailer,
LoggerInterface $logger,
Environment $template,
CrmManager $crmManager
) {
$this->attachment_dir = $attachment_dir;
$this->mailer = $mailer;
$this->logger = $logger;
$this->template = $template;
$this->crmManager = $crmManager;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ReviewEvent::CREATE_REVIEW => [ 'createReview' ],
ReviewEvent::HOST_CREATE_REVIEW => ['hostCreateReview']
];
}
public function createReview(ReviewEvent $reviewEvent)
{
$review = $reviewEvent->getReview();
$this->crmManager->markAsReviewed($review);
$message = ( new \Swift_Message() )
->setSubject('Nouvel avis client sur votre offre : '.$review->getExperience()->getTitle())
->setTo([ $review->getHost()->getEmail() ])
->setCc([ '[email protected]', '[email protected]', '[email protected]' ])
->setBcc([ '[email protected]', '[email protected]' ])
->setFrom([ '[email protected]' => 'Isère Attractivité' ])
->setBody(
$this->template->render('email/clientreview.html.twig', [
'review' => $review,
]),
'text/html'
)
->attach(\Swift_Attachment::fromPath($this->attachment_dir."/Bien répondre aux avis clients.pdf"))
;
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Error sending email', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]);
}
}
public function hostCreateReview(ReviewEvent $reviewEvent)
{
$review = $reviewEvent->getReview();
$message = ( new \Swift_Message() )
->setSubject('L\'hébergeur '.$review->getHost()->getFirstname().' '.$review->getHost()->getLastname().' a répondu à un avis sur son offre - Evasion.ishere.fr')
->setTo(['[email protected]', '[email protected]', '[email protected]'])
->setBcc([ '[email protected]' ])
->setFrom([ '[email protected]' => 'Admin Plateforme Isère Attractivité' ])
->setBody(
$this->template->render('email/hostreview.html.twig', [
'review' => $review,
]),
'text/html'
);
try {
$this->mailer->send($message);
} catch (\Exception $e) {
$this->logger->error('Error sending email', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]);
}
}
}