src/WellCommerce/Bundle/AppBundle/EventListener/SyneriseSubscriber.php line 46

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\AppBundle\EventListener;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Messenger\Stamp\DelayStamp;
  16. use WellCommerce\Bundle\AppBundle\Entity\Client;
  17. use WellCommerce\Bundle\AppBundle\Service\Messenger\SyneriseClientMessage;
  18. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  19. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  20. use WellCommerce\Component\Form\Event\FormEvent;
  21. /**
  22.  * Class SyneriseSubscriber
  23.  *
  24.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  25.  */
  26. class SyneriseSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  27. {
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'client.post_create'                         => ['onClientPostUpdate'10],
  32.             'client.post_update'                         => ['onClientPostUpdate'10],
  33.             'front.client_register.pre_form_init'        => 'onRegisterFormInit',
  34.             'front.client_login.pre_form_init'           => 'onLoginFormInit',
  35.             'front.client_reset_password.pre_form_init'  => 'onResetPasswordFormInit',
  36.             'front.contact.pre_form_init'                => 'onContactFormInit',
  37.             'front.address.pre_form_init'                => 'onAddressFormInit',
  38.             'front.client_billing_address.pre_form_init' => 'onBillingAddressFormInit',
  39.             'front.client_contact_details.pre_form_init' => 'onContactDetailsFormInit',
  40.         ];
  41.     }
  42.     public function onAddressFormInit(FormEvent $event)
  43.     {
  44.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  45.             $form $event->getForm();
  46.             $form->setOption('synerise''address');
  47.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise''firstname');
  48.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise''lastname');
  49.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise''phone');
  50.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.email')->setOption('synerise''email');
  51.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.postalCode')->setOption('synerise''zipCode');
  52.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.city')->setOption('synerise''city');
  53.             $form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.sex')->setOption('synerise''gender');
  54.             $form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.newsletterAccepted')->setOption('synerise''newsletterAgreement');
  55.         }
  56.     }
  57.     public function onBillingAddressFormInit(FormEvent $event)
  58.     {
  59.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  60.             $form $event->getForm();
  61.             $form->setOption('synerise''client_billing_address');
  62.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.firstName')->setOption('synerise''firstname');
  63.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.lastName')->setOption('synerise''lastname');
  64.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.postalCode')->setOption('synerise''zipCode');
  65.             $form->getChildren()->get('billingAddress')->getChildren()->get('billingAddress.city')->setOption('synerise''city');
  66.         }
  67.     }
  68.     public function onContactDetailsFormInit(FormEvent $event)
  69.     {
  70.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  71.             $form $event->getForm();
  72.             $form->setOption('synerise''client_contact_details');
  73.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise''firstname');
  74.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise''lastname');
  75.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise''phone');
  76.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.email')->setOption('synerise''email');
  77.         }
  78.     }
  79.     public function onContactFormInit(FormEvent $event)
  80.     {
  81.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  82.             $form $event->getForm();
  83.             $form->setOption('synerise''contact');
  84.             $form->getChildren()->get('name')->setOption('synerise''firstname');
  85.             $form->getChildren()->get('surname')->setOption('synerise''lastname');
  86.             $form->getChildren()->get('phone')->setOption('synerise''phone');
  87.             $form->getChildren()->get('email')->setOption('synerise''email');
  88.         }
  89.     }
  90.     public function onRegisterFormInit(FormEvent $event)
  91.     {
  92.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  93.             $form $event->getForm();
  94.             $form->setOption('synerise''client_register');
  95.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.firstName')->setOption('synerise''firstname');
  96.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.lastName')->setOption('synerise''lastname');
  97.             $form->getChildren()->get('contactDetails')->getChildren()->get('contactDetails.phone')->setOption('synerise''phone');
  98.             $form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.sex')->setOption('synerise''sex');
  99.             $form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.username')->setOption('synerise''email');
  100.             $form->getChildren()->get('clientDetails')->getChildren()->get('clientDetails.newsletterAccepted')->setOption('synerise''newsletterAgreement');
  101.         }
  102.     }
  103.     public function onLoginFormInit(FormEvent $event)
  104.     {
  105.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  106.             $form $event->getForm();
  107.             $form->setOption('synerise''client_login');
  108.             $form->getChildren()->get('_username')->setOption('synerise''email');
  109.         }
  110.     }
  111.     public function onResetPasswordFormInit(FormEvent $event)
  112.     {
  113.         if ($this->getShopStorage()->getCurrentShop()->isSyneriseClientSync()) {
  114.             $form $event->getForm();
  115.             $form->setOption('synerise''client_reset_password');
  116.             $form->getChildren()->get('_username')->setOption('synerise''email');
  117.         }
  118.     }
  119.     public function onClientPostUpdate(EntityEvent $event)
  120.     {
  121.         $entity $event->getEntity();
  122.         if ($entity instanceof Client) {
  123.             $this->getMessageBus()->dispatch(new SyneriseClientMessage($entity->getId()), [
  124.                 new DelayStamp(1000),
  125.             ]);
  126.         }
  127.     }
  128. }