src/WellCommerce/Bundle/MailChimpBundle/EventListener/MailChimpSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\MailChimpBundle\EventListener;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use WellCommerce\Bundle\AppBundle\Entity\NewsletterSubscriber;
  16. use WellCommerce\Bundle\AppBundle\Entity\Shop;
  17. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  18. use WellCommerce\Bundle\MailChimpBundle\Service\MailChimpClient;
  19. use WellCommerce\Component\Form\Event\FormEvent;
  20. class MailChimpSubscriber implements EventSubscriberInterface
  21. {
  22.     protected MailChimpClient $mailChimpClient;
  23.     public function __construct(MailChimpClient $mailChimpClient)
  24.     {
  25.         $this->mailChimpClient $mailChimpClient;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             'admin.shop.pre_form_init'          => ['onShopFormAdminInit'],
  31.             'newsletter_subscriber.post_create' => ['onNewsletterSubscriberPostCreate'],
  32.             'newsletter_subscriber.post_update' => ['onNewsletterSubscriberPostUpdate'],
  33.         ];
  34.     }
  35.     public function onNewsletterSubscriberPostCreate(EntityEvent $event)
  36.     {
  37.         $resource $event->getEntity();
  38.         if ($resource instanceof NewsletterSubscriber && $resource->isEnabled()) {
  39.             $shop $resource->getShop();
  40.             if ($this->mailChimpClient->isConfigured($shop)) {
  41.                 $email  $resource->getEmail();
  42.                 $listId $shop->getMailChimpListId();
  43.                 $this->mailChimpClient->getClient($shop)->lists->addListMember($listId, [
  44.                     "email_address" => $email,
  45.                     "status"        => "subscribed",
  46.                 ]);
  47.             }
  48.         }
  49.     }
  50.     public function onNewsletterSubscriberPostUpdate(EntityEvent $event)
  51.     {
  52.         $resource $event->getEntity();
  53.         if ($resource instanceof NewsletterSubscriber) {
  54.             $shop $resource->getShop();
  55.             if ($this->mailChimpClient->isConfigured($shop)) {
  56.                 $listId $shop->getMailChimpListId();
  57.                 $email  $resource->getEmail();
  58.                 if ($resource->isEnabled()) {
  59.                     $this->mailChimpClient->getClient($shop)->lists->addListMember($listId, [
  60.                         "email_address" => $email,
  61.                         "status"        => "subscribed",
  62.                     ]);
  63.                 } else {
  64.                     $subscriberHash md5(strtolower($email));
  65.                     $this->mailChimpClient->getClient($shop)->lists->deleteListMember($listId$subscriberHash);
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     public function onShopFormAdminInit(FormEvent $event)
  71.     {
  72.         $resource $event->getResource();
  73.         if ($resource instanceof Shop) {
  74.             $form    $event->getForm();
  75.             $builder $event->getFormBuilder();
  76.             $mailChimpData $form->addChild($builder->getElement('nested_fieldset', [
  77.                 'name'  => 'mail_chimp_data',
  78.                 'label' => 'mail_chimp.fieldset.settings',
  79.             ]));
  80.             $mailChimpData->addChild($builder->getElement('text_field', [
  81.                 'name'  => 'mailChimpApiKey',
  82.                 'label' => 'mail_chimp.label.api_key',
  83.             ]));
  84.             $mailChimpData->addChild($builder->getElement('text_field', [
  85.                 'name'  => 'mailChimpServerPrefix',
  86.                 'label' => 'mail_chimp.label.server_prefix',
  87.             ]));
  88.             $mailChimpData->addChild($builder->getElement('text_field', [
  89.                 'name'  => 'mailChimpListId',
  90.                 'label' => 'mail_chimp.label.list_id',
  91.             ]));
  92.             $mailChimpData->addChild($builder->getElement('text_field', [
  93.                 'name'  => 'mailChimpClientId',
  94.                 'label' => 'mail_chimp.label.client_id',
  95.             ]));
  96.             $mailChimpData->addChild($builder->getElement('text_field', [
  97.                 'name'  => 'mailChimpClientSecret',
  98.                 'label' => 'mail_chimp.label.client_secret',
  99.             ]));
  100.             $mailChimpData->addChild($builder->getElement('text_field', [
  101.                 'name'  => 'mailChimpGoalsId',
  102.                 'label' => 'mail_chimp.label.goals_id',
  103.             ]));
  104.         }
  105.     }
  106. }