<?php
namespace App\EventSubscriber\User;
use App\Entity\User\User;
use App\Manager\Boutique\PanierManager;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ProfileSubscriber implements EventSubscriberInterface
{
private $router;
private $session;
private $panierManager;
public function __construct(UrlGeneratorInterface $router, SessionInterface $session, PanierManager $panierManager) {
$this->router = $router;
$this->session = $session;
$this->panierManager = $panierManager;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::PROFILE_EDIT_SUCCESS => array('onProfileEditSuccess', -10),
);
}
public function onProfileEditSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
// On MAJ le client du panier si on en a un
$panierId = $this->session->get('panierId');
if (!is_null($panierId)) {
$this->panierManager->modifierUser($user);
if (!is_null($user->getPays())) {
$this->panierManager->modifierLivraisonPays($user, $user->getPays());
}
}
$redirect = $this->router->generate('fos_user_profile_show');
$response = new RedirectResponse($redirect);
$event->setResponse($response);
}
}