src/Aqarmap/Bundle/UserBundle/EventListener/UserUpdateListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\UserBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Contracts\PhoneManagerInterface;
  4. use Aqarmap\Bundle\UserBundle\Entity\User;
  5. use Aqarmap\Bundle\UserBundle\Services\Contracts\UserPhoneManagerInterface;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use FOS\UserBundle\Event\FormEvent;
  8. use FOS\UserBundle\FOSUserEvents;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class UserUpdateListener implements EventSubscriberInterface
  11. {
  12.     private EntityManagerInterface $entityManager;
  13.     private PhoneManagerInterface $phoneManager;
  14.     private UserPhoneManagerInterface $userPhoneManager;
  15.     public function __construct(
  16.         PhoneManagerInterface $phoneManager,
  17.         EntityManagerInterface $entityManager,
  18.         UserPhoneManagerInterface $userPhoneManager
  19.     ) {
  20.         $this->phoneManager $phoneManager;
  21.         $this->entityManager $entityManager;
  22.         $this->userPhoneManager $userPhoneManager;
  23.     }
  24.     public function onProfileEditSuccess(FormEvent $event): void
  25.     {
  26.         $mainPhones = [
  27.             $event->getForm()->get('firstPhone')->getData(),
  28.             $event->getForm()->get('secondPhone')->getData(),
  29.         ];
  30.         $countryCodes = [
  31.             $event->getForm()->get('firstCountryCode')->getData(),
  32.             $event->getForm()->get('secondCountryCode')->getData(),
  33.         ];
  34.         $hasWhatsApp $event->getRequest()->request->get('hasWhatsApp');
  35.         $phones $this->preparePhones($mainPhones$countryCodes);
  36.         /** @var User $user */
  37.         $user $event->getForm()->getData();
  38.         $this->userPhoneManager->addMainPhoneNumbers($user$phones$countryCodes$hasWhatsApp);
  39.         $user->setMigratedPhone(true);
  40.         $user->setPhoneNumber($user->getPhoneNumber());
  41.         $this->entityManager->persist($user);
  42.         $this->entityManager->flush();
  43.         try {
  44.             $this->checkPhoneDuplications($mainPhones$countryCodes);
  45.         } catch (\Exception $exception) {
  46.         }
  47.     }
  48.     private function checkPhoneDuplications(array $mainPhones, array $countryCodes): void
  49.     {
  50.         foreach ($mainPhones as $index => $mainPhone) {
  51.             if (!$mainPhone) {
  52.                 continue;
  53.             }
  54.             $mainPhone $countryCodes[$index].$mainPhone;
  55.             $mainPhone $this->phoneManager->trimZero($mainPhone$countryCodes[$index]);
  56.             $phones[] = $mainPhone;
  57.         }
  58.     }
  59.     /**
  60.      * @return array
  61.      */
  62.     private function preparePhones(array $mainPhones, array $countryCodes)
  63.     {
  64.         $preparedPhones = [];
  65.         foreach ($mainPhones as $index => $mainPhone) {
  66.             if (!$mainPhone) {
  67.                 continue; // Skip empty phone numbers
  68.             }
  69.             // Trim leading zeros
  70.             $mainPhone $this->phoneManager->trimZero($mainPhone$countryCodes[$index]);
  71.             // Add prepared phone to the list
  72.             $preparedPhones[] = $mainPhone;
  73.         }
  74.         return $preparedPhones;
  75.     }
  76.     public static function getSubscribedEvents()
  77.     {
  78.         return [
  79.             FOSUserEvents::PROFILE_EDIT_SUCCESS => [
  80.                 ['onProfileEditSuccess'],
  81.             ],
  82.         ];
  83.     }
  84. }