<?php
namespace App\EventListener;
use App\Entity\CustomerHistoryEntry;
use App\Repository\PersonRepository;
use Doctrine\ORM\EntityManagerInterface;
use Menke\UserBundle\Event\UserActivatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserSubscriber implements EventSubscriberInterface
{
private $manager;
private $personRepo;
public function __construct(EntityManagerInterface $manager, PersonRepository $personRepo)
{
$this->manager = $manager;
$this->personRepo = $personRepo;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
UserActivatedEvent::NAME => 'onUserActivated',
];
}
/**
* @return array
*/
public function onUserActivated(UserActivatedEvent $event)
{
$user = $event->getUser();
$person = $this->personRepo->findOneBy(['user' => $user]);
if ($person) {
$historyEntry = new CustomerHistoryEntry(
'Konto aktiviert',
$person,
'System'
);
$this->manager->persist($historyEntry);
$this->manager->flush();
}
}
}