src/EventListener/UserSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\CustomerHistoryEntry;
  4. use App\Repository\PersonRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Menke\UserBundle\Event\UserActivatedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class UserSubscriber implements EventSubscriberInterface
  9. {
  10.     private $manager;
  11.     private $personRepo;
  12.     public function __construct(EntityManagerInterface $managerPersonRepository $personRepo)
  13.     {
  14.         $this->manager $manager;
  15.         $this->personRepo $personRepo;
  16.     }
  17.     /**
  18.      * @return array
  19.      */
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             UserActivatedEvent::NAME => 'onUserActivated',
  24.         ];
  25.     }
  26.     /**
  27.      * @return array
  28.      */
  29.     public function onUserActivated(UserActivatedEvent $event)
  30.     {
  31.         $user $event->getUser();
  32.         $person $this->personRepo->findOneBy(['user' => $user]);
  33.         if ($person) {
  34.             $historyEntry = new CustomerHistoryEntry(
  35.                 'Konto aktiviert',
  36.                 $person,
  37.                 'System'
  38.             );
  39.             $this->manager->persist($historyEntry);
  40.             $this->manager->flush();
  41.         }
  42.     }
  43. }