vendor/sylius/sylius/src/Sylius/Bundle/UserBundle/EventListener/PasswordUpdaterListener.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\UserBundle\EventListener;
  12. use Doctrine\ORM\Event\LifecycleEventArgs;
  13. use Sylius\Component\User\Model\UserInterface;
  14. use Sylius\Component\User\Security\PasswordUpdaterInterface;
  15. use Symfony\Component\EventDispatcher\GenericEvent;
  16. class PasswordUpdaterListener
  17. {
  18.     private PasswordUpdaterInterface $passwordUpdater;
  19.     public function __construct(PasswordUpdaterInterface $passwordUpdater)
  20.     {
  21.         $this->passwordUpdater $passwordUpdater;
  22.     }
  23.     public function genericEventUpdater(GenericEvent $event): void
  24.     {
  25.         $this->updatePassword($event->getSubject());
  26.     }
  27.     public function prePersist(LifecycleEventArgs $event): void
  28.     {
  29.         $user $event->getEntity();
  30.         if (!$user instanceof UserInterface) {
  31.             return;
  32.         }
  33.         $this->updatePassword($user);
  34.     }
  35.     public function preUpdate(LifecycleEventArgs $event): void
  36.     {
  37.         $user $event->getEntity();
  38.         if (!$user instanceof UserInterface) {
  39.             return;
  40.         }
  41.         $this->updatePassword($user);
  42.     }
  43.     protected function updatePassword(UserInterface $user): void
  44.     {
  45.         if (null !== $user->getPlainPassword()) {
  46.             $this->passwordUpdater->updatePassword($user);
  47.         }
  48.     }
  49. }