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

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