src/EventListener/ReviewSubscriber.php line 71

Open in your IDE?
  1. <?php declare( strict_types=);
  2. namespace App\EventListener;
  3. use App\Event\ReviewEvent;
  4. use App\Manager\CrmManager;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Twig\Environment;
  8. class ReviewSubscriber implements EventSubscriberInterface
  9. {
  10.     private $attachment_dir;
  11.     private $mailer;
  12.     private $logger;
  13.     private $template;
  14.     private $crmManager;
  15.     public function __construct(
  16.         $attachment_dir,
  17.         \Swift_Mailer $mailer,
  18.         LoggerInterface $logger,
  19.         Environment $template,
  20.         CrmManager $crmManager
  21.     ) {
  22.         $this->attachment_dir $attachment_dir;
  23.         $this->mailer   $mailer;
  24.         $this->logger   $logger;
  25.         $this->template $template;
  26.         $this->crmManager $crmManager;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             ReviewEvent::CREATE_REVIEW => [ 'createReview' ],
  35.             ReviewEvent::HOST_CREATE_REVIEW => ['hostCreateReview']
  36.         ];
  37.     }
  38.     public function createReview(ReviewEvent $reviewEvent)
  39.     {
  40.         $review  $reviewEvent->getReview();
  41.         $this->crmManager->markAsReviewed($review);
  42.         $message = ( new \Swift_Message() )
  43.             ->setSubject('Nouvel avis client sur votre offre : '.$review->getExperience()->getTitle())
  44.             ->setTo([ $review->getHost()->getEmail() ])
  45.             ->setCc([ '[email protected]''[email protected]''[email protected]])
  46.             ->setBcc([ '[email protected]''[email protected]])
  47.             ->setFrom([ '[email protected]=> 'Isère Attractivité' ])
  48.             ->setBody(
  49.                 $this->template->render('email/clientreview.html.twig', [
  50.                     'review' => $review,
  51.                 ]),
  52.                 'text/html'
  53.             )
  54.             ->attach(\Swift_Attachment::fromPath($this->attachment_dir."/Bien répondre aux avis clients.pdf"))
  55.         ;
  56.         try {
  57.             $this->mailer->send($message);
  58.         } catch (\Exception $e) {
  59.             $this->logger->error('Error sending email', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]);
  60.         }
  61.     }
  62.     public function hostCreateReview(ReviewEvent $reviewEvent)
  63.     {
  64.         $review  $reviewEvent->getReview();
  65.         $message = ( new \Swift_Message() )
  66.             ->setSubject('L\'hébergeur '.$review->getHost()->getFirstname().' '.$review->getHost()->getLastname().' a répondu à un avis sur son offre - Evasion.ishere.fr')
  67.             ->setTo(['[email protected]''[email protected]''[email protected]'])
  68.             ->setBcc([ '[email protected]])
  69.             ->setFrom([ '[email protected]=> 'Admin Plateforme Isère Attractivité' ])
  70.             ->setBody(
  71.                 $this->template->render('email/hostreview.html.twig', [
  72.                     'review' => $review,
  73.                 ]),
  74.                 'text/html'
  75.             );
  76.         try {
  77.             $this->mailer->send($message);
  78.         } catch (\Exception $e) {
  79.             $this->logger->error('Error sending email', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]);
  80.         }
  81.     }
  82. }