<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\MailChimpBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\NewsletterSubscriber;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\MailChimpBundle\Service\MailChimpClient;
use WellCommerce\Component\Form\Event\FormEvent;
class MailChimpSubscriber implements EventSubscriberInterface
{
protected MailChimpClient $mailChimpClient;
public function __construct(MailChimpClient $mailChimpClient)
{
$this->mailChimpClient = $mailChimpClient;
}
public static function getSubscribedEvents()
{
return [
'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
'newsletter_subscriber.post_create' => ['onNewsletterSubscriberPostCreate'],
'newsletter_subscriber.post_update' => ['onNewsletterSubscriberPostUpdate'],
];
}
public function onNewsletterSubscriberPostCreate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof NewsletterSubscriber && $resource->isEnabled()) {
$shop = $resource->getShop();
if ($this->mailChimpClient->isConfigured($shop)) {
$email = $resource->getEmail();
$listId = $shop->getMailChimpListId();
$this->mailChimpClient->getClient($shop)->lists->addListMember($listId, [
"email_address" => $email,
"status" => "subscribed",
]);
}
}
}
public function onNewsletterSubscriberPostUpdate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof NewsletterSubscriber) {
$shop = $resource->getShop();
if ($this->mailChimpClient->isConfigured($shop)) {
$listId = $shop->getMailChimpListId();
$email = $resource->getEmail();
if ($resource->isEnabled()) {
$this->mailChimpClient->getClient($shop)->lists->addListMember($listId, [
"email_address" => $email,
"status" => "subscribed",
]);
} else {
$subscriberHash = md5(strtolower($email));
$this->mailChimpClient->getClient($shop)->lists->deleteListMember($listId, $subscriberHash);
}
}
}
}
public function onShopFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Shop) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$mailChimpData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'mail_chimp_data',
'label' => 'mail_chimp.fieldset.settings',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpApiKey',
'label' => 'mail_chimp.label.api_key',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpServerPrefix',
'label' => 'mail_chimp.label.server_prefix',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpListId',
'label' => 'mail_chimp.label.list_id',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpClientId',
'label' => 'mail_chimp.label.client_id',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpClientSecret',
'label' => 'mail_chimp.label.client_secret',
]));
$mailChimpData->addChild($builder->getElement('text_field', [
'name' => 'mailChimpGoalsId',
'label' => 'mail_chimp.label.goals_id',
]));
}
}
}