core/lib/Thelia/Action/Newsletter.php line 65

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\Newsletter\NewsletterEvent;
  15. use Thelia\Core\Event\TheliaEvents;
  16. use Thelia\Mailer\MailerFactory;
  17. use Thelia\Model\ConfigQuery;
  18. use Thelia\Model\Newsletter as NewsletterModel;
  19. use Thelia\Model\NewsletterQuery;
  20. /**
  21.  * Class Newsletter.
  22.  *
  23.  * @author Manuel Raynaud <manu@raynaud.io>
  24.  */
  25. class Newsletter extends BaseAction implements EventSubscriberInterface
  26. {
  27.     /** @var MailerFactory */
  28.     protected $mailer;
  29.     /** @var EventDispatcherInterface */
  30.     protected $dispatcher;
  31.     public function __construct(MailerFactory $mailerEventDispatcherInterface $dispatcher)
  32.     {
  33.         $this->mailer $mailer;
  34.         $this->dispatcher $dispatcher;
  35.     }
  36.     public function subscribe(NewsletterEvent $event): void
  37.     {
  38.         // test if the email is already registered and unsubscribed
  39.         if (null === $newsletter NewsletterQuery::create()->findOneByEmail($event->getEmail())) {
  40.             $newsletter = new NewsletterModel();
  41.         }
  42.         $newsletter
  43.             ->setEmail($event->getEmail())
  44.             ->setFirstname($event->getFirstname())
  45.             ->setLastname($event->getLastname())
  46.             ->setLocale($event->getLocale())
  47.             ->setUnsubscribed(false)
  48.             ->save();
  49.         $event->setNewsletter($newsletter);
  50.         if (ConfigQuery::getNotifyNewsletterSubscription()) {
  51.             $this->dispatcher->dispatch($eventTheliaEvents::NEWSLETTER_CONFIRM_SUBSCRIPTION);
  52.         }
  53.     }
  54.     public function unsubscribe(NewsletterEvent $event): void
  55.     {
  56.         if (null !== $nl NewsletterQuery::create()->findPk($event->getId())) {
  57.             $nl
  58.                 ->setUnsubscribed(true)
  59.                 ->save();
  60.             $event->setNewsletter($nl);
  61.         }
  62.     }
  63.     public function update(NewsletterEvent $event): void
  64.     {
  65.         if (null !== $nl NewsletterQuery::create()->findPk($event->getId())) {
  66.             $nl->setEmail($event->getEmail())
  67.                 ->setFirstname($event->getFirstname())
  68.                 ->setLastname($event->getLastname())
  69.                 ->setUnsubscribed(false)
  70.                 ->setLocale($event->getLocale())
  71.                 ->save();
  72.             $event->setNewsletter($nl);
  73.         }
  74.     }
  75.     /**
  76.      * @since 2.3.0-alpha2
  77.      */
  78.     public function confirmSubscription(NewsletterEvent $event): void
  79.     {
  80.         $this->mailer->sendEmailMessage(
  81.             'newsletter_subscription_confirmation',
  82.             [ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName()],
  83.             [$event->getEmail() => $event->getFirstname().' '.$event->getLastname()],
  84.             [
  85.                 'email' => $event->getEmail(),
  86.                 'firstname' => $event->getFirstname(),
  87.                 'lastname' => $event->getLastname(),
  88.             ],
  89.             $event->getLocale()
  90.         );
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public static function getSubscribedEvents()
  96.     {
  97.         return [
  98.             TheliaEvents::NEWSLETTER_SUBSCRIBE => ['subscribe'128],
  99.             TheliaEvents::NEWSLETTER_UPDATE => ['update'128],
  100.             TheliaEvents::NEWSLETTER_UNSUBSCRIBE => ['unsubscribe'128],
  101.             TheliaEvents::NEWSLETTER_CONFIRM_SUBSCRIPTION => ['confirmSubscription'128],
  102.         ];
  103.     }
  104. }