custom/plugins/s360-device-detection/src/Subscriber/DeviceSubscriber.php line 73

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace S360DeviceDetection\Subscriber;
  4. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\Struct\ArrayStruct;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  21. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  22. use S360DeviceDetection\Subscriber\Mobile_Detect;
  23. class DeviceSubscriber implements EventSubscriberInterface {
  24.     /**
  25.      * @var RequestStack
  26.      */
  27.     private $request_stack;
  28.     /**
  29.      * @var Session
  30.      */
  31.     protected $session;
  32.     const SESSION_KEY 'deviceType';
  33.     public function __construct (
  34.         RequestStack $request_stack,
  35.         Session $session
  36.     ) {
  37.         $this->request_stack $request_stack;
  38.         $this->session $session;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             GenericPageLoadedEvent::class => 'addDeviceToSession'
  44.         ];
  45.     }
  46.     public function addDeviceToSession() {
  47.         $request $this->request_stack->getCurrentRequest();
  48.         if (!$request) {
  49.             return;
  50.         }
  51.         $device = new Mobile_Detect;
  52.         $session $request->getSession();
  53.         if($session){
  54.             if($device->isMobile()){
  55.                 $session->set(self::SESSION_KEY"mobile");
  56.             } elseif ($device->isTablet()){
  57.                 $session->set(self::SESSION_KEY"tablet");
  58.             } else {
  59.                 $session->set(self::SESSION_KEY"desktop");
  60.             }
  61.         }
  62.     }
  63. }