vendor/thelia/core/lib/Thelia/Action/Customer.php line 64

Open in your IDE?
  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 Symfony\Component\HttpFoundation\RequestStack;
  15. use Thelia\Core\Event\ActionEvent;
  16. use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
  17. use Thelia\Core\Event\Customer\CustomerLoginEvent;
  18. use Thelia\Core\Event\LostPasswordEvent;
  19. use Thelia\Core\Event\TheliaEvents;
  20. use Thelia\Core\Security\SecurityContext;
  21. use Thelia\Core\Translation\Translator;
  22. use Thelia\Exception\CustomerException;
  23. use Thelia\Mailer\MailerFactory;
  24. use Thelia\Model\ConfigQuery;
  25. use Thelia\Model\Customer as CustomerModel;
  26. use Thelia\Model\CustomerQuery;
  27. use Thelia\Model\Event\CustomerEvent;
  28. use Thelia\Model\LangQuery;
  29. use Thelia\Tools\Password;
  30. /**
  31.  * customer class where all actions are managed.
  32.  *
  33.  * Class Customer
  34.  *
  35.  * @author Manuel Raynaud <manu@raynaud.io>
  36.  */
  37. class Customer extends BaseAction implements EventSubscriberInterface
  38. {
  39.     /** @var SecurityContext */
  40.     protected $securityContext;
  41.     /** @var MailerFactory */
  42.     protected $mailer;
  43.     /** @var RequestStack */
  44.     protected $requestStack;
  45.     public function __construct(SecurityContext $securityContextMailerFactory $mailerRequestStack $requestStack null)
  46.     {
  47.         $this->securityContext $securityContext;
  48.         $this->mailer $mailer;
  49.         $this->requestStack $requestStack;
  50.     }
  51.     /**
  52.      * @param $eventName
  53.      *
  54.      * @throws \Propel\Runtime\Exception\PropelException
  55.      */
  56.     public function create(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  57.     {
  58.         $customer = new CustomerModel();
  59.         $plainPassword $event->getPassword();
  60.         $this->createOrUpdateCustomer($customer$event$dispatcher);
  61.         if ($event->getNotifyCustomerOfAccountCreation()) {
  62.             $this->mailer->sendEmailToCustomer(
  63.                 'customer_account_created',
  64.                 $customer,
  65.                 ['password' => $plainPassword]
  66.             );
  67.         }
  68.         $dispatcher->dispatch(
  69.             new CustomerEvent($customer),
  70.             TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL
  71.         );
  72.     }
  73.     public function customerConfirmationEmail(CustomerEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  74.     {
  75.         $customer $event->getModel();
  76.         if (ConfigQuery::isCustomerEmailConfirmationEnable() && $customer->getConfirmationToken() !== null) {
  77.             $this->mailer->sendEmailToCustomer(
  78.                 'customer_confirmation',
  79.                 $customer,
  80.                 ['customer_id' => $customer->getId()]
  81.             );
  82.         }
  83.     }
  84.     /**
  85.      * @param $eventName
  86.      *
  87.      * @throws \Propel\Runtime\Exception\PropelException
  88.      */
  89.     public function modify(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  90.     {
  91.         $plainPassword $event->getPassword();
  92.         $customer $event->getCustomer();
  93.         $emailChanged $customer->getEmail() !== $event->getEmail();
  94.         $this->createOrUpdateCustomer($customer$event$dispatcher);
  95.         if ($event->getNotifyCustomerOfAccountModification() && (!empty($plainPassword) || $emailChanged)) {
  96.             $this->mailer->sendEmailToCustomer('customer_account_changed'$customer, ['password' => $plainPassword]);
  97.         }
  98.     }
  99.     /**
  100.      * @param $eventName
  101.      *
  102.      * @throws \Propel\Runtime\Exception\PropelException
  103.      */
  104.     public function updateProfile(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  105.     {
  106.         $customer $event->getCustomer();
  107.         if ($event->getTitle() !== null) {
  108.             $customer->setTitleId($event->getTitle());
  109.         }
  110.         if ($event->getFirstname() !== null) {
  111.             $customer->setFirstname($event->getFirstname());
  112.         }
  113.         if ($event->getLastname() !== null) {
  114.             $customer->setLastname($event->getLastname());
  115.         }
  116.         if ($event->getEmail() !== null) {
  117.             $customer->setEmail($event->getEmail(), $event->getEmailUpdateAllowed());
  118.         }
  119.         if ($event->getPassword() !== null) {
  120.             $customer->setPassword($event->getPassword());
  121.         }
  122.         if ($event->getReseller() !== null) {
  123.             $customer->setReseller($event->getReseller());
  124.         }
  125.         if ($event->getSponsor() !== null) {
  126.             $customer->setSponsor($event->getSponsor());
  127.         }
  128.         if ($event->getDiscount() !== null) {
  129.             $customer->setDiscount($event->getDiscount());
  130.         }
  131.         if ($event->getLangId() !== null) {
  132.             $customer->setLangId($event->getLangId());
  133.         }
  134.         $customer->save();
  135.         $event->setCustomer($customer);
  136.     }
  137.     /**
  138.      * @throws \Propel\Runtime\Exception\PropelException
  139.      */
  140.     public function delete(CustomerEvent $event): void
  141.     {
  142.         if (null !== $customer $event->getModel()) {
  143.             if (true === $customer->hasOrder()) {
  144.                 throw new CustomerException(Translator::getInstance()->trans('Impossible to delete a customer who already have orders'));
  145.             }
  146.             $customer->delete();
  147.         }
  148.     }
  149.     /**
  150.      * @throws \Propel\Runtime\Exception\PropelException
  151.      */
  152.     private function createOrUpdateCustomer(CustomerModel $customerCustomerCreateOrUpdateEvent $eventEventDispatcherInterface $dispatcher): void
  153.     {
  154.         $customer->createOrUpdate(
  155.             $event->getTitle(),
  156.             $event->getFirstname(),
  157.             $event->getLastname(),
  158.             $event->getAddress1(),
  159.             $event->getAddress2(),
  160.             $event->getAddress3(),
  161.             $event->getPhone(),
  162.             $event->getCellphone(),
  163.             $event->getZipcode(),
  164.             $event->getCity(),
  165.             $event->getCountry(),
  166.             $event->getEmail(),
  167.             $event->getPassword(),
  168.             $event->getLangId(),
  169.             $event->getReseller(),
  170.             $event->getSponsor(),
  171.             $event->getDiscount(),
  172.             $event->getCompany(),
  173.             $event->getRef(),
  174.             $event->getEmailUpdateAllowed(),
  175.             $event->getState()
  176.         );
  177.         $event->setCustomer($customer);
  178.     }
  179.     public function login(CustomerLoginEvent $event): void
  180.     {
  181.         $customer $event->getCustomer();
  182.         if (method_exists($customer'clearDispatcher')) {
  183.             $customer->clearDispatcher();
  184.         }
  185.         $this->securityContext->setCustomerUser($event->getCustomer());
  186.         // Set the preferred customer language
  187.         if (null !== $this->requestStack
  188.             &&
  189.             !empty($customer->getLangId())
  190.             &&
  191.             (null !== $lang LangQuery::create()->findPk($customer->getLangId()))
  192.         ) {
  193.             $this->requestStack->getCurrentRequest()->getSession()->setLang($lang);
  194.         }
  195.     }
  196.     /**
  197.      * Perform user logout. The user is redirected to the provided view, if any.
  198.      */
  199.     public function logout(/* @noinspection PhpUnusedParameterInspection */ ActionEvent $event): void
  200.     {
  201.         $this->securityContext->clearCustomerUser();
  202.     }
  203.     /**
  204.      * @throws \Propel\Runtime\Exception\PropelException
  205.      */
  206.     public function lostPassword(LostPasswordEvent $event): void
  207.     {
  208.         if (null !== $customer CustomerQuery::create()->filterByEmail($event->getEmail())->findOne()) {
  209.             $password Password::generateRandom(8);
  210.             $customer
  211.                 ->setPassword($password)
  212.                 ->save()
  213.             ;
  214.             $this->mailer->sendEmailToCustomer('lost_password'$customer, ['password' => $password]);
  215.         }
  216.     }
  217.     /**
  218.      * {@inheritdoc}
  219.      */
  220.     public static function getSubscribedEvents()
  221.     {
  222.         return [
  223.             TheliaEvents::CUSTOMER_CREATEACCOUNT => ['create'128],
  224.             TheliaEvents::CUSTOMER_UPDATEACCOUNT => ['modify'128],
  225.             TheliaEvents::CUSTOMER_UPDATEPROFILE => ['updateProfile'128],
  226.             TheliaEvents::CUSTOMER_LOGOUT => ['logout'128],
  227.             TheliaEvents::CUSTOMER_LOGIN => ['login'128],
  228.             TheliaEvents::CUSTOMER_DELETEACCOUNT => ['delete'128],
  229.             TheliaEvents::LOST_PASSWORD => ['lostPassword'128],
  230.             TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL => ['customerConfirmationEmail'128],
  231.         ];
  232.     }
  233. }