vendor/suncat/mobile-detect-bundle/SunCat/MobileDetectBundle/Helper/DeviceView.php line 113

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the MobileDetectBundle.
  4.  *
  5.  * (c) Nikolay Ivlev <nikolay.kotovsky@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. namespace SunCat\MobileDetectBundle\Helper;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. /**
  16.  * DeviceView
  17.  *
  18.  * @author suncat2000 <nikolay.kotovsky@gmail.com>
  19.  */
  20. class DeviceView
  21. {
  22.     const VIEW_MOBILE       'mobile';
  23.     const VIEW_TABLET       'tablet';
  24.     const VIEW_FULL         'full';
  25.     const VIEW_NOT_MOBILE   'not_mobile';
  26.     const COOKIE_KEY_DEFAULT                      'device_view';
  27.     const COOKIE_PATH_DEFAULT                     '/';
  28.     const COOKIE_DOMAIN_DEFAULT                   '';
  29.     const COOKIE_SECURE_DEFAULT                   false;
  30.     const COOKIE_HTTP_ONLY_DEFAULT                true;
  31.     const COOKIE_RAW_DEFAULT                      false;
  32.     const COOKIE_SAMESITE_DEFAULT                 null;
  33.     const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT '1 month';
  34.     const SWITCH_PARAM_DEFAULT                    'device_view';
  35.     /**
  36.      * @var Request
  37.      */
  38.     protected $request;
  39.     /**
  40.      * @var string
  41.      */
  42.     protected $requestedViewType;
  43.     /**
  44.      * @var string
  45.      */
  46.     protected $viewType;
  47.     /**
  48.      * @var string
  49.      */
  50.     protected $cookieKey self::COOKIE_KEY_DEFAULT;
  51.     /**
  52.      * @var string
  53.      */
  54.     protected $cookiePath self::COOKIE_PATH_DEFAULT;
  55.     /**
  56.      * @var string
  57.      */
  58.     protected $cookieDomain self::COOKIE_DOMAIN_DEFAULT;
  59.     /**
  60.      * @var bool
  61.      */
  62.     protected $cookieSecure self::COOKIE_SECURE_DEFAULT;
  63.     /**
  64.      * @var bool
  65.      */
  66.     protected $cookieHttpOnly self::COOKIE_HTTP_ONLY_DEFAULT;
  67.     
  68.     /**
  69.      * @var bool
  70.      */
  71.     protected $cookieRaw self::COOKIE_RAW_DEFAULT;
  72.     /**
  73.      * @var string|null
  74.      */
  75.     protected $cookieSamesite self::COOKIE_SAMESITE_DEFAULT;
  76.     /**
  77.      * @var string
  78.      */
  79.     protected $cookieExpireDatetimeModifier self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT;
  80.     /**
  81.      * @var string
  82.      */
  83.     protected $switchParam self::SWITCH_PARAM_DEFAULT;
  84.     /**
  85.      * @var array
  86.      */
  87.     protected $redirectConfig;
  88.     /**
  89.      * Constructor
  90.      *
  91.      * @param RequestStack $requestStack
  92.      */
  93.     public function __construct(RequestStack $requestStack null)
  94.     {
  95.         if (!$requestStack || !$this->request $requestStack->getMasterRequest()) {
  96.             $this->viewType self::VIEW_NOT_MOBILE;
  97.             return;
  98.         }
  99.         if ($this->request->query->has($this->switchParam)) {
  100.             $this->viewType $this->request->query->get($this->switchParam);
  101.         } elseif ($this->request->cookies->has($this->cookieKey)) {
  102.             $this->viewType $this->request->cookies->get($this->cookieKey);
  103.         }
  104.         $this->requestedViewType $this->viewType;
  105.     }
  106.     /**
  107.      * Gets the view type for a device.
  108.      *
  109.      * @return string
  110.      */
  111.     public function getViewType()
  112.     {
  113.         return $this->viewType;
  114.     }
  115.     /**
  116.      * Gets the view type that has explicitly been requested either by switch param, or by cookie.
  117.      *
  118.      * @return string The requested view type or null if no view type has been explicitly requested.
  119.      */
  120.     public function getRequestedViewType()
  121.     {
  122.         return $this->requestedViewType;
  123.     }
  124.     /**
  125.      * Is the device in full view.
  126.      *
  127.      * @return boolean
  128.      */
  129.     public function isFullView()
  130.     {
  131.         return $this->viewType === self::VIEW_FULL;
  132.     }
  133.     /**
  134.      * Is the device a tablet view type.
  135.      *
  136.      * @return boolean
  137.      */
  138.     public function isTabletView()
  139.     {
  140.         return $this->viewType === self::VIEW_TABLET;
  141.     }
  142.     /**
  143.      * Is the device a mobile view type.
  144.      *
  145.      * @return boolean
  146.      */
  147.     public function isMobileView()
  148.     {
  149.         return $this->viewType === self::VIEW_MOBILE;
  150.     }
  151.     /**
  152.      * Is not the device a mobile view type (PC, Mac, etc.).
  153.      *
  154.      * @return boolean
  155.      */
  156.     public function isNotMobileView()
  157.     {
  158.         return $this->viewType === self::VIEW_NOT_MOBILE;
  159.     }
  160.     /**
  161.      * Has the Request the switch param in the query string (GET header).
  162.      *
  163.      * @return boolean
  164.      */
  165.     public function hasSwitchParam()
  166.     {
  167.         return $this->request && $this->request->query->has($this->switchParam);
  168.     }
  169.     /**
  170.      * Sets the view type.
  171.      *
  172.      * @param string $view
  173.      */
  174.     public function setView($view)
  175.     {
  176.         $this->viewType $view;
  177.     }
  178.     /**
  179.      * Sets the full (desktop) view type.
  180.      */
  181.     public function setFullView()
  182.     {
  183.         $this->viewType self::VIEW_FULL;
  184.     }
  185.     /**
  186.      * Sets the tablet view type.
  187.      */
  188.     public function setTabletView()
  189.     {
  190.         $this->viewType self::VIEW_TABLET;
  191.     }
  192.     /**
  193.      * Sets the mobile view type.
  194.      */
  195.     public function setMobileView()
  196.     {
  197.         $this->viewType self::VIEW_MOBILE;
  198.     }
  199.     /**
  200.      * Sets the not mobile view type.
  201.      */
  202.     public function setNotMobileView()
  203.     {
  204.         $this->viewType self::VIEW_NOT_MOBILE;
  205.     }
  206.     /**
  207.      * Gets the switch param value from the query string (GET header).
  208.      *
  209.      * @return string|null
  210.      */
  211.     public function getSwitchParamValue()
  212.     {
  213.         if (!$this->request) {
  214.             return null;
  215.         }
  216.         return $this->request->query->get($this->switchParamself::VIEW_FULL);
  217.     }
  218.     /**
  219.      * Getter of RedirectConfig.
  220.      *
  221.      * @return array
  222.      */
  223.     public function getRedirectConfig()
  224.     {
  225.         return $this->redirectConfig;
  226.     }
  227.     /**
  228.      * Setter of RedirectConfig.
  229.      *
  230.      * @param array $redirectConfig
  231.      */
  232.     public function setRedirectConfig($redirectConfig)
  233.     {
  234.         $this->redirectConfig $redirectConfig;
  235.     }
  236.     /**
  237.      * Gets the RedirectResponse by switch param value.
  238.      *
  239.      * @param string $redirectUrl
  240.      *
  241.      * @return RedirectResponseWithCookie
  242.      */
  243.     public function getRedirectResponseBySwitchParam($redirectUrl)
  244.     {
  245.         switch ($this->getSwitchParamValue()) {
  246.             case self::VIEW_MOBILE:
  247.                 $viewType self::VIEW_MOBILE;
  248.                 break;
  249.             case self::VIEW_TABLET:
  250.                 $viewType self::VIEW_TABLET;
  251.                 if (isset($this->redirectConfig['detect_tablet_as_mobile']) && $this->redirectConfig['detect_tablet_as_mobile'] === true) {
  252.                     $viewType self::VIEW_MOBILE;
  253.                 }
  254.                 break;
  255.             default:
  256.                 $viewType self::VIEW_FULL;
  257.         }
  258.         return new RedirectResponseWithCookie($redirectUrl$this->getStatusCode($viewType), $this->createCookie($viewType));
  259.     }
  260.     /**
  261.      * Modifies the Response for the specified device view.
  262.      *
  263.      * @param string   $view     The device view for which the response should be modified.
  264.      * @param Response $response
  265.      *
  266.      * @return Response
  267.      */
  268.     public function modifyResponse($viewResponse $response)
  269.     {
  270.         $response->headers->setCookie($this->createCookie($view));
  271.         return $response;
  272.     }
  273.     /**
  274.      * Gets the RedirectResponse for the specified device view.
  275.      *
  276.      * @param string $view       The device view for which we want the RedirectResponse.
  277.      * @param string $host       Uri host
  278.      * @param int    $statusCode Status code
  279.      *
  280.      * @return RedirectResponseWithCookie
  281.      */
  282.     public function getRedirectResponse($view$host$statusCode)
  283.     {
  284.         return new RedirectResponseWithCookie($host$statusCode$this->createCookie($view));
  285.     }
  286.     /**
  287.      * Setter of CookieKey
  288.      *
  289.      * @param string $cookieKey
  290.      */
  291.     public function setCookieKey($cookieKey)
  292.     {
  293.         $this->cookieKey $cookieKey;
  294.     }
  295.     /**
  296.      * Getter of CookieKey
  297.      *
  298.      * @return string
  299.      */
  300.     public function getCookieKey()
  301.     {
  302.         return $this->cookieKey;
  303.     }
  304.     /**
  305.      * Getter of CookiePath.
  306.      *
  307.      * @return string
  308.      */
  309.     public function getCookiePath()
  310.     {
  311.         return $this->cookiePath;
  312.     }
  313.     /**
  314.      * Setter of CookiePath.
  315.      *
  316.      * @param string $cookiePath
  317.      */
  318.     public function setCookiePath($cookiePath)
  319.     {
  320.         $this->cookiePath $cookiePath;
  321.     }
  322.     /**
  323.      * Getter of CookieDomain.
  324.      *
  325.      * @return string
  326.      */
  327.     public function getCookieDomain()
  328.     {
  329.         return $this->cookieDomain;
  330.     }
  331.     /**
  332.      * Setter of CookieDomain.
  333.      *
  334.      * @param string $cookieDomain
  335.      */
  336.     public function setCookieDomain($cookieDomain)
  337.     {
  338.         $this->cookieDomain $cookieDomain;
  339.     }
  340.     /**
  341.      * Is the cookie secure.
  342.      *
  343.      * @return bool
  344.      */
  345.     public function isCookieSecure()
  346.     {
  347.         return $this->cookieSecure;
  348.     }
  349.     /**
  350.      * Setter of CookieSecure.
  351.      *
  352.      * @param bool $cookieSecure
  353.      */
  354.     public function setCookieSecure($cookieSecure)
  355.     {
  356.         $this->cookieSecure $cookieSecure;
  357.     }
  358.     /**
  359.      * Is the cookie http only.
  360.      *
  361.      * @return bool
  362.      */
  363.     public function isCookieHttpOnly()
  364.     {
  365.         return $this->cookieHttpOnly;
  366.     }
  367.     /**
  368.      * Setter of CookieHttpOnly.
  369.      *
  370.      * @param bool $cookieHttpOnly
  371.      */
  372.     public function setCookieHttpOnly($cookieHttpOnly)
  373.     {
  374.         $this->cookieHttpOnly $cookieHttpOnly;
  375.     }
  376.     /**
  377.      * Is the cookie raw.
  378.      *
  379.      * @return bool
  380.      */
  381.     public function isCookieRaw()
  382.     {
  383.         return $this->cookieRaw;
  384.     }
  385.     /**
  386.      * Setter of CookieRaw.
  387.      *
  388.      * @param bool $cookieRaw
  389.      */
  390.     public function setCookieRaw($cookieRaw)
  391.     {
  392.         $this->cookieRaw $cookieRaw;
  393.     }
  394.     /**
  395.      * Getter of CookieSamesite.
  396.      *
  397.      * @return string|null
  398.      */
  399.     public function getCookieSamesite()
  400.     {
  401.         return $this->cookieSamesite;
  402.     }
  403.     /**
  404.      * Setter of CookieSamesite.
  405.      *
  406.      * @param string|null $cookieSamesite
  407.      */
  408.     public function setCookieSamesite($cookieSamesite)
  409.     {
  410.         $this->cookieSamesite $cookieSamesite;
  411.     }
  412.     /**
  413.      * Setter of SwitchParam.
  414.      *
  415.      * @param string $switchParam
  416.      */
  417.     public function setSwitchParam($switchParam)
  418.     {
  419.         $this->switchParam $switchParam;
  420.     }
  421.     /**
  422.      * Getter of SwitchParam
  423.      *
  424.      * @return string
  425.      */
  426.     public function getSwitchParam()
  427.     {
  428.         return $this->switchParam;
  429.     }
  430.     /**
  431.      * @param string $cookieExpireDatetimeModifier
  432.      */
  433.     public function setCookieExpireDatetimeModifier($cookieExpireDatetimeModifier)
  434.     {
  435.         $this->cookieExpireDatetimeModifier $cookieExpireDatetimeModifier;
  436.     }
  437.     /**
  438.      * @return string
  439.      */
  440.     public function getCookieExpireDatetimeModifier()
  441.     {
  442.         return $this->cookieExpireDatetimeModifier;
  443.     }
  444.     /**
  445.      * Create the Cookie object
  446.      *
  447.      * @param string $value
  448.      *
  449.      * @return Cookie
  450.      */
  451.     protected function createCookie($value)
  452.     {
  453.         try {
  454.             $expire = new \Datetime($this->getCookieExpireDatetimeModifier());
  455.         } catch (\Exception $e) {
  456.             $expire = new \Datetime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
  457.         }
  458.         return new Cookie(
  459.             $this->getCookieKey(),
  460.             $value,
  461.             $expire,
  462.             $this->getCookiePath(),
  463.             $this->getCookieDomain(),
  464.             $this->isCookieSecure(),
  465.             $this->isCookieHttpOnly(),
  466.             $this->isCookieRaw(),
  467.             $this->getCookieSamesite()
  468.         );
  469.     }
  470.     /**
  471.      * @param string $view
  472.      *
  473.      * @return integer
  474.      */
  475.     protected function getStatusCode($view)
  476.     {
  477.         if (isset($this->redirectConfig[$view]['status_code'])) {
  478.             return $this->redirectConfig[$view]['status_code'];
  479.         }
  480.         return 302;
  481.     }
  482. }