vendor/api-platform/core/src/Bridge/FosUser/EventListener.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\FosUser;
  12. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  13. use FOS\UserBundle\Model\UserInterface;
  14. use FOS\UserBundle\Model\UserManagerInterface;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. /**
  17.  * Bridges between FOSUserBundle and API Platform Core.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  * @author Théo FIDRY <theo.fidry@gmail.com>
  21.  */
  22. final class EventListener
  23. {
  24.     private $userManager;
  25.     public function __construct(UserManagerInterface $userManager)
  26.     {
  27.         $this->userManager $userManager;
  28.     }
  29.     /**
  30.      * Persists, updates or delete data return by the controller if applicable.
  31.      */
  32.     public function onKernelView(ViewEvent $event): void
  33.     {
  34.         $request $event->getRequest();
  35.         if (!RequestAttributesExtractor::extractAttributes($request)) {
  36.             return;
  37.         }
  38.         $user $event->getControllerResult();
  39.         if (!$user instanceof UserInterface || $request->isMethodSafe()) {
  40.             return;
  41.         }
  42.         if ('DELETE' === $request->getMethod()) {
  43.             $this->userManager->deleteUser($user);
  44.             $event->setControllerResult(null);
  45.         } else {
  46.             $this->userManager->updateUser($user);
  47.         }
  48.     }
  49. }