<?php
namespace Aqarmap\Bundle\UserBundle\EventListener;
use Aqarmap\Bundle\UserBundle\Entity\User;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class ProfileEditedListener implements EventSubscriberInterface
{
private UserManagerInterface $userManager;
public function __construct(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}
public function onProfileEdited(FilterUserResponseEvent $event): void
{
$formFiles = $event->getRequest()->files->get('fos_user_profile_form');
/** @var User $user */
$user = $event->getUser();
$response = $event->getResponse();
if ((isset($formFiles['logo']['file']) && $this->handleLogo($formFiles['logo']['file'], $user, $response))
|| (isset($formFiles['personalPhoto']['file']) && $this->handlePersonalPhoto($formFiles['personalPhoto']['file'], $user, $response))) {
$this->userManager->updateUser($user);
}
}
/**
* @return Response|null
*/
private function handleLogo($logo, User $user, Response $response)
{
if ($logo) {
$user->setIsValidLogo(false);
$time = time() + (86400 * 30);
$response->headers->setCookie(new Cookie('hide_logo_request', true, $time));
$response->sendHeaders();
return $response;
}
return null;
}
/**
* @return Response|null
*/
private function handlePersonalPhoto($personalPhoto, User $user, Response $response)
{
if ($personalPhoto) {
$user->setIsValidPersonalPhoto(false);
$time = time() + (86400 * 30);
$response->headers->setCookie(new Cookie('hide_profile_picture_request', true, $time));
$response->sendHeaders();
return $response;
}
return null;
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::PROFILE_EDIT_COMPLETED => 'onProfileEdited',
];
}
}