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

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