core/lib/Thelia/Action/Customer.php line 100

  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.      * @throws \Propel\Runtime\Exception\PropelException
  53.      */
  54.     public function create(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  55.     {
  56.         $customer = new CustomerModel();
  57.         $plainPassword $event->getPassword();
  58.         $this->createOrUpdateCustomer($customer$event$dispatcher);
  59.         if ($event->getNotifyCustomerOfAccountCreation()) {
  60.             $this->mailer->sendEmailToCustomer(
  61.                 'customer_account_created',
  62.                 $customer,
  63.                 ['password' => $plainPassword]
  64.             );
  65.         }
  66.         $dispatcher->dispatch(
  67.             new CustomerEvent($customer),
  68.             TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL
  69.         );
  70.     }
  71.     public function customerConfirmationEmail(CustomerEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  72.     {
  73.         $customer $event->getModel();
  74.         if (ConfigQuery::isCustomerEmailConfirmationEnable() && $customer->getConfirmationToken() !== null) {
  75.             $this->mailer->sendEmailToCustomer(
  76.                 'customer_confirmation',
  77.                 $customer,
  78.                 ['customer_id' => $customer->getId()]
  79.             );
  80.         }
  81.     }
  82.     /**
  83.      * @throws \Propel\Runtime\Exception\PropelException
  84.      */
  85.     public function modify(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  86.     {
  87.         $plainPassword $event->getPassword();
  88.         $customer $event->getCustomer();
  89.         $emailChanged $customer->getEmail() !== $event->getEmail();
  90.         $this->createOrUpdateCustomer($customer$event$dispatcher);
  91.         if ($event->getNotifyCustomerOfAccountModification() && (!empty($plainPassword) || $emailChanged)) {
  92.             $this->mailer->sendEmailToCustomer('customer_account_changed'$customer, ['password' => $plainPassword]);
  93.         }
  94.     }
  95.     /**
  96.      * @throws \Propel\Runtime\Exception\PropelException
  97.      */
  98.     public function updateProfile(CustomerCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  99.     {
  100.         $customer $event->getCustomer();
  101.         if ($event->getTitle() !== null) {
  102.             $customer->setTitleId($event->getTitle());
  103.         }
  104.         if ($event->getFirstname() !== null) {
  105.             $customer->setFirstname($event->getFirstname());
  106.         }
  107.         if ($event->getLastname() !== null) {
  108.             $customer->setLastname($event->getLastname());
  109.         }
  110.         if ($event->getEmail() !== null) {
  111.             $customer->setEmail($event->getEmail(), $event->getEmailUpdateAllowed());
  112.         }
  113.         if ($event->getPassword() !== null) {
  114.             $customer->setPassword($event->getPassword());
  115.         }
  116.         if ($event->getReseller() !== null) {
  117.             $customer->setReseller($event->getReseller());
  118.         }
  119.         if ($event->getSponsor() !== null) {
  120.             $customer->setSponsor($event->getSponsor());
  121.         }
  122.         if ($event->getDiscount() !== null) {
  123.             $customer->setDiscount($event->getDiscount());
  124.         }
  125.         if ($event->getLangId() !== null) {
  126.             $customer->setLangId($event->getLangId());
  127.         }
  128.         $customer->save();
  129.         $event->setCustomer($customer);
  130.     }
  131.     /**
  132.      * @throws \Propel\Runtime\Exception\PropelException
  133.      */
  134.     public function delete(CustomerEvent $event): void
  135.     {
  136.         if (null !== $customer $event->getModel()) {
  137.             if (true === $customer->hasOrder()) {
  138.                 throw new CustomerException(Translator::getInstance()->trans('Impossible to delete a customer who already have orders'));
  139.             }
  140.             $customer->delete();
  141.         }
  142.     }
  143.     /**
  144.      * @throws \Propel\Runtime\Exception\PropelException
  145.      */
  146.     private function createOrUpdateCustomer(CustomerModel $customerCustomerCreateOrUpdateEvent $eventEventDispatcherInterface $dispatcher): void
  147.     {
  148.         $customer->createOrUpdate(
  149.             $event->getTitle(),
  150.             $event->getFirstname(),
  151.             $event->getLastname(),
  152.             $event->getAddress1(),
  153.             $event->getAddress2(),
  154.             $event->getAddress3(),
  155.             $event->getPhone(),
  156.             $event->getCellphone(),
  157.             $event->getZipcode(),
  158.             $event->getCity(),
  159.             $event->getCountry(),
  160.             $event->getEmail(),
  161.             $event->getPassword(),
  162.             $event->getLangId(),
  163.             $event->getReseller(),
  164.             $event->getSponsor(),
  165.             $event->getDiscount(),
  166.             $event->getCompany(),
  167.             $event->getRef(),
  168.             $event->getEmailUpdateAllowed(),
  169.             $event->getState()
  170.         );
  171.         $event->setCustomer($customer);
  172.     }
  173.     public function login(CustomerLoginEvent $event): void
  174.     {
  175.         $customer $event->getCustomer();
  176.         if (method_exists($customer'clearDispatcher')) {
  177.             $customer->clearDispatcher();
  178.         }
  179.         $this->securityContext->setCustomerUser($event->getCustomer());
  180.         // Set the preferred customer language
  181.         if (null !== $this->requestStack
  182.             &&
  183.             !empty($customer->getLangId())
  184.             &&
  185.             (null !== $lang LangQuery::create()->findPk($customer->getLangId()))
  186.         ) {
  187.             $this->requestStack->getCurrentRequest()->getSession()->setLang($lang);
  188.         }
  189.     }
  190.     /**
  191.      * Perform user logout. The user is redirected to the provided view, if any.
  192.      */
  193.     public function logout(/* @noinspection PhpUnusedParameterInspection */ ActionEvent $event): void
  194.     {
  195.         $this->securityContext->clearCustomerUser();
  196.     }
  197.     /**
  198.      * @throws \Propel\Runtime\Exception\PropelException
  199.      */
  200.     public function lostPassword(LostPasswordEvent $event): void
  201.     {
  202.         if (null !== $customer CustomerQuery::create()->filterByEmail($event->getEmail())->findOne()) {
  203.             $password Password::generateRandom(8);
  204.             $customer
  205.                 ->setPassword($password)
  206.                 ->save()
  207.             ;
  208.             $this->mailer->sendEmailToCustomer('lost_password'$customer, ['password' => $password]);
  209.         }
  210.     }
  211.     /**
  212.      * {@inheritdoc}
  213.      */
  214.     public static function getSubscribedEvents()
  215.     {
  216.         return [
  217.             TheliaEvents::CUSTOMER_CREATEACCOUNT => ['create'128],
  218.             TheliaEvents::CUSTOMER_UPDATEACCOUNT => ['modify'128],
  219.             TheliaEvents::CUSTOMER_UPDATEPROFILE => ['updateProfile'128],
  220.             TheliaEvents::CUSTOMER_LOGOUT => ['logout'128],
  221.             TheliaEvents::CUSTOMER_LOGIN => ['login'128],
  222.             TheliaEvents::CUSTOMER_DELETEACCOUNT => ['delete'128],
  223.             TheliaEvents::LOST_PASSWORD => ['lostPassword'128],
  224.             TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL => ['customerConfirmationEmail'128],
  225.         ];
  226.     }
  227. }