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

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.     const SESSION_KEY 'deviceType';
  29.     public function __construct (
  30.         RequestStack $request_stack
  31.     ) {
  32.         $this->request_stack $request_stack;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             GenericPageLoadedEvent::class => 'addDeviceToSession'
  38.         ];
  39.     }
  40.     public function addDeviceToSession() {
  41.         $request $this->request_stack->getCurrentRequest();
  42.         if (!$request) {
  43.             return;
  44.         }
  45.         $device = new Mobile_Detect;
  46.         $session $request->getSession();
  47.         if($session){
  48.             if($device->isMobile()){
  49.                 $session->set(self::SESSION_KEY"mobile");
  50.             } elseif ($device->isTablet()){
  51.                 $session->set(self::SESSION_KEY"tablet");
  52.             } else {
  53.                 $session->set(self::SESSION_KEY"desktop");
  54.             }
  55.         }
  56.     }
  57. }