src/Sylius/Bundle/UserBundle/EventListener/PasswordUpdaterListener.php line 31

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.     /** @var PasswordUpdaterInterface */
  19.     private $passwordUpdater;
  20.     public function __construct(PasswordUpdaterInterface $passwordUpdater)
  21.     {
  22.         $this->passwordUpdater $passwordUpdater;
  23.     }
  24.     public function genericEventUpdater(GenericEvent $event): void
  25.     {
  26.         $this->updatePassword($event->getSubject());
  27.     }
  28.     public function prePersist(LifecycleEventArgs $event): void
  29.     {
  30.         $user $event->getEntity();
  31.         if (!$user instanceof UserInterface) {
  32.             return;
  33.         }
  34.         $this->updatePassword($user);
  35.     }
  36.     public function preUpdate(LifecycleEventArgs $event): void
  37.     {
  38.         $user $event->getEntity();
  39.         if (!$user instanceof UserInterface) {
  40.             return;
  41.         }
  42.         $this->updatePassword($user);
  43.     }
  44.     protected function updatePassword(UserInterface $user): void
  45.     {
  46.         if (null !== $user->getPlainPassword()) {
  47.             $this->passwordUpdater->updatePassword($user);
  48.         }
  49.     }
  50. }