<?php
namespace App\EventSubscriber;
use App\Entity\Doctrine\App\History;
use App\Entity\Doctrine\App\User;
use App\Event\LdapPasswordUpdateEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class LdapPasswordSubscriber
* @package App\EventSubscriber
*/
class LdapPasswordSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* ApiLdapPasswordSubscriber constructor.
* @param EntityManagerInterface $em
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(EntityManagerInterface $em, TokenStorageInterface $tokenStorage)
{
$this->em = $em;
$this->tokenStorage = $tokenStorage;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
LdapPasswordUpdateEvent::class => ['postUpdate']
];
}
/**
* @param LdapPasswordUpdateEvent $event
*/
public function postUpdate(LdapPasswordUpdateEvent $event)
{
$user = $event->getUser();
if (!$user instanceof User) {
return;
}
$history = new History();
$history->setUser($this->tokenStorage->getToken()->getUser())
->setCommon($user)
->setDate(new \DateTime())
->setAction(\App\Entity\History::ACTION_USER_LDAP_PASSWORD_UPDATED);
$this->em->persist($history);
$this->em->flush();
}
}