custom/static-plugins/SamsonCustomer/src/Subscriber/StorefrontSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2024
  11.  *
  12.  ***/
  13. use Samson\Exception\CustomerNeedsPasswordChangeException;
  14. use Shopware\Core\SalesChannelRequest;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Routing\RouterInterface;
  20. class StorefrontSubscriber implements EventSubscriberInterface
  21. {
  22.     private RouterInterface $router;
  23.     public function __construct(RouterInterface $router)
  24.     {
  25.         $this->router $router;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::EXCEPTION => [
  31.                 ['customerNeedsPasswordChangeHandler'],
  32.             ]
  33.         ];
  34.     }
  35.     public function customerNeedsPasswordChangeHandler(ExceptionEvent $event)
  36.     {
  37.         if (!$event->getRequest()->attributes->has(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
  38.             return;
  39.         }
  40.         if (!$event->getThrowable() instanceof CustomerNeedsPasswordChangeException) {
  41.             return;
  42.         }
  43.         $request $event->getRequest();
  44.         $parameters = [
  45.             'redirectTo' => $request->attributes->get('_route'),
  46.             'redirectParameters' => json_encode($request->attributes->get('_route_params')),
  47.         ];
  48.         $redirectResponse = new RedirectResponse($this->router->generate('frontend.sub-account.password.page'$parameters));
  49.         $event->setResponse($redirectResponse);
  50.     }
  51. }