<?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\AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Service\Messenger\SyneriseClientMessage;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class SyneriseSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class SyneriseSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'client.post_create' => ['onClientPostUpdate', 10],
'client.post_update' => ['onClientPostUpdate', 10],
'front.client_register.pre_form_init' => 'onRegisterFormInit',
'front.client_login.pre_form_init' => 'onLoginFormInit',
'front.client_reset_password.pre_form_init' => 'onResetPasswordFormInit',
'front.contact.pre_form_init' => 'onContactFormInit',
'front.address.pre_form_init' => 'onAddressFormInit',
'front.client_billing_address.pre_form_init' => 'onBillingAddressFormInit',
'front.client_contact_details.pre_form_init' => 'onContactDetailsFormInit',
];
}
public function onAddressFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'address');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise', 'firstname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise', 'lastname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise', 'phone');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.email')->setOption('synerise', 'email');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.postalCode')->setOption('synerise', 'zipCode');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.city')->setOption('synerise', 'city');
$form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.sex')->setOption('synerise', 'gender');
$form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.newsletterAccepted')->setOption('synerise', 'newsletterAgreement');
}
}
public function onBillingAddressFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'client_billing_address');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.firstName')->setOption('synerise', 'firstname');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.lastName')->setOption('synerise', 'lastname');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.postalCode')->setOption('synerise', 'zipCode');
$form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.city')->setOption('synerise', 'city');
}
}
public function onContactDetailsFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'client_contact_details');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise', 'firstname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise', 'lastname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise', 'phone');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.email')->setOption('synerise', 'email');
}
}
public function onContactFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'contact');
$form->getChildren()->get('name')->setOption('synerise', 'firstname');
$form->getChildren()->get('surname')->setOption('synerise', 'lastname');
$form->getChildren()->get('phone')->setOption('synerise', 'phone');
$form->getChildren()->get('email')->setOption('synerise', 'email');
}
}
public function onRegisterFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'client_register');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise', 'firstname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise', 'lastname');
$form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise', 'phone');
$form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.sex')->setOption('synerise', 'sex');
$form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.username')->setOption('synerise', 'email');
$form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.newsletterAccepted')->setOption('synerise', 'newsletterAgreement');
}
}
public function onLoginFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'client_login');
$form->getChildren()->get('_username')->setOption('synerise', 'email');
}
}
public function onResetPasswordFormInit(FormEvent $event)
{
if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
$form = $event->getForm();
$form->setOption('synerise', 'client_reset_password');
$form->getChildren()->get('_username')->setOption('synerise', 'email');
}
}
public function onClientPostUpdate(EntityEvent $event)
{
$entity = $event->getEntity();
if ($entity instanceof Client) {
$this->getMessageBus()->dispatch(new SyneriseClientMessage($entity->getId()), [
new DelayStamp(1000),
]);
}
}
}