Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

InitUserMerchantEventSubscriber.php 2.0KB

3 роки тому
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  5. use Lc\SovBundle\Doctrine\EntityInterface;
  6. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  7. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  8. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  9. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  10. use Lc\SovBundle\Model\User\UserInterface;
  11. use Lc\SovBundle\Repository\AbstractRepositoryInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. class InitUserMerchantEventSubscriber implements EventSubscriberInterface
  16. {
  17. protected $em;
  18. protected $userMerchantFactory;
  19. public function __construct(EntityManagerInterface $entityManager, UserMerchantFactory $userMerchantFactory)
  20. {
  21. $this->em = $entityManager;
  22. $this->userMerchantFactory = $userMerchantFactory;
  23. }
  24. public static function getSubscribedEvents()
  25. {
  26. return [
  27. EntityManagerEvent::PRE_CREATE_EVENT => ['createUserMerchant'],
  28. ];
  29. }
  30. public function createUserMerchant(EntityManagerEvent $event)
  31. {
  32. $user = $event->getEntity();
  33. $entityRepository = $this->em->getRepository(get_class($user));
  34. if ($user instanceof UserInterface) {
  35. $existingUser = $entityRepository->findOneByEmail($user->getEmail());
  36. //Le user n'existe pas, on le créer et on lui créer un user_merchant
  37. if ($existingUser == null) {
  38. $userMerchant = $this->userMerchantFactory->create(
  39. [
  40. 'active' => true,
  41. 'roles' => $user->getRoles(),
  42. 'user' => $user
  43. ]
  44. );
  45. $this->em->create($userMerchant);
  46. }
  47. }
  48. }
  49. }