custom/plugins/HatslogicImplifyExtended/src/Subscriber/AccountSubscriber.php line 97

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3. * (c) 2Hats Logic Solutions <info@2hatslogic.com>
  4. */
  5. namespace Hatslogic\ImplifyExtended\Subscriber;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Doctrine\DBAL\Connection;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  15. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  16. use Symfony\Component\HttpFoundation\Session\Session;
  17. class AccountSubscriber implements EventSubscriberInterface
  18. {
  19. /**
  20. * @var ContainerInterface
  21. */
  22. protected $container;
  23. /**
  24. * @var Connection
  25. */
  26. private $connection;
  27. /**
  28. * @var RouterInterface
  29. */
  30. private $router;
  31. /**
  32. * @var RequestStack
  33. */
  34. private $requestStack;
  35. /** @var ResponseEvent */
  36. private $event;
  37. /**
  38. * @var Session
  39. */
  40. private $session;
  41. public function __construct(
  42. ContainerInterface $container,
  43. Connection $connection,
  44. RouterInterface $router,
  45. RequestStack $requestStack,
  46. Session $session
  47. ) {
  48. $this->container = $container;
  49. $this->connection = $connection;
  50. $this->router = $router;
  51. $this->requestStack = $requestStack;
  52. $this->session = $session;
  53. }
  54. public static function getSubscribedEvents(): array
  55. {
  56. return [
  57. KernelEvents::REQUEST => 'kernelEventsRequest',
  58. CustomerLoginEvent::class => 'afterSuccessfulLogin',
  59. CustomerLogoutEvent::class => 'afterLogout'
  60. ];
  61. }
  62. public function afterSuccessfulLogin(CustomerLoginEvent $event)
  63. {
  64. $this->session->set('customerLoggedIn', true);
  65. return;
  66. }
  67. public function afterLogout(CustomerLogoutEvent $event)
  68. {
  69. if($this->session->has('customerLoggedIn')) {
  70. $this->session->remove('customerLoggedIn');
  71. }
  72. return;
  73. }
  74. public function kernelEventsRequest(RequestEvent $event)
  75. {
  76. $request = $event->getRequest();
  77. $attributes = $request->attributes;
  78. $route = $attributes->get('_route');
  79. if($this->session->has('customerLoggedIn') && $route == 'frontend.account.login.page')
  80. {
  81. echo "<script type='text/javascript'> document.location = '".$this->router->generate('frontend.account.home.page')."'; </script>";
  82. exit;
  83. }
  84. return;
  85. }
  86. }