Skip to content

Commit

Permalink
Merge pull request #113 from orions/fix/checkstyle
Browse files Browse the repository at this point in the history
Fix Checkstyle (Symfony Rules)
  • Loading branch information
suncat2000 authored Jun 26, 2017
2 parents de6bfe6 + bb97031 commit 4e10425
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
30 changes: 13 additions & 17 deletions DataCollector/DeviceDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class DeviceDataCollector extends DataCollector
{
/**
* @var \SunCat\MobileDetectBundle\Helper\DeviceView
* @var DeviceView
*/
protected $deviceView;

Expand All @@ -38,7 +38,7 @@ class DeviceDataCollector extends DataCollector
/**
* DeviceDataCollector constructor.
*
* @param \SunCat\MobileDetectBundle\Helper\DeviceView $deviceView Device View Detector
* @param DeviceView $deviceView Device View Detector
*/
public function __construct(DeviceView $deviceView)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public function collect(
DeviceView::VIEW_FULL
),
'isCurrent' => $this->deviceView->isFullView(),
'enabled' => $this->canUseView(DeviceView::VIEW_FULL, $request->getSchemeAndHttpHost())
'enabled' => $this->canUseView(DeviceView::VIEW_FULL, $request->getSchemeAndHttpHost()),
),
array(
'type' => DeviceView::VIEW_TABLET,
Expand All @@ -79,7 +79,7 @@ public function collect(
DeviceView::VIEW_TABLET
),
'isCurrent' => $this->deviceView->isTabletView(),
'enabled' => $this->canUseView(DeviceView::VIEW_TABLET, $request->getSchemeAndHttpHost())
'enabled' => $this->canUseView(DeviceView::VIEW_TABLET, $request->getSchemeAndHttpHost()),
),
array(
'type' => DeviceView::VIEW_MOBILE,
Expand All @@ -89,7 +89,7 @@ public function collect(
DeviceView::VIEW_MOBILE
),
'isCurrent' => $this->deviceView->isMobileView(),
'enabled' => $this->canUseView(DeviceView::VIEW_MOBILE, $request->getSchemeAndHttpHost())
'enabled' => $this->canUseView(DeviceView::VIEW_MOBILE, $request->getSchemeAndHttpHost()),
),
);
}
Expand Down Expand Up @@ -133,41 +133,37 @@ public function getName()
/**
* @param $view
* @param $host
*
* @return bool
*/
protected function canUseView($view, $host)
{
if (!is_array($this->redirectConfig)) {
return true;
}

if (!isset($this->redirectConfig[$view])) {
return true;
}

if (
!isset($this->redirectConfig[$view]['is_enabled']) ||
if (!isset($this->redirectConfig[$view]['is_enabled']) ||
false === $this->redirectConfig[$view]['is_enabled']
) {
return true;
}

if (
true === $this->redirectConfig[$view]['is_enabled'] &&
if (true === $this->redirectConfig[$view]['is_enabled'] &&
isset($this->redirectConfig[$view]['host']) &&
isset($this->redirectConfig[$view]['action']) &&
!empty($this->redirectConfig[$view]['host']) &&
in_array($this->redirectConfig[$view]['action'], [
RequestResponseListener::REDIRECT, RequestResponseListener::REDIRECT_WITHOUT_PATH
]
)
in_array($this->redirectConfig[$view]['action'], [RequestResponseListener::REDIRECT, RequestResponseListener::REDIRECT_WITHOUT_PATH])
) {
$parseHost = parse_url($this->redirectConfig[$view]['host']);
$redirectHost = $parseHost['scheme'] . '://' . $parseHost['host'];
$redirectHost = $parseHost['scheme'].'://'.$parseHost['host'];
if (!empty($parseHost['port'])) {
$redirectHost .= ':' . $parseHost['port'];
$redirectHost .= ':'.$parseHost['port'];
}

if ($redirectHost !== $host) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/MobileDetectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('mobile_detect.xml');
$loader->load('listener.xml');
$loader->load('twig.xml');
Expand Down
53 changes: 26 additions & 27 deletions EventListener/RequestResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector;
use SunCat\MobileDetectBundle\Helper\DeviceView;
use SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand All @@ -28,13 +30,13 @@
*/
class RequestResponseListener
{
CONST REDIRECT = 'redirect';
CONST NO_REDIRECT = 'no_redirect';
CONST REDIRECT_WITHOUT_PATH = 'redirect_without_path';
const REDIRECT = 'redirect';
const NO_REDIRECT = 'no_redirect';
const REDIRECT_WITHOUT_PATH = 'redirect_without_path';

CONST MOBILE = 'mobile';
CONST TABLET = 'tablet';
CONST FULL = 'full';
const MOBILE = 'mobile';
const TABLET = 'tablet';
const FULL = 'full';

/**
* @var MobileDetector
Expand Down Expand Up @@ -81,8 +83,7 @@ public function __construct(
RouterInterface $router,
array $redirectConf,
$fullPath = true
)
{
) {
$this->mobileDetector = $mobileDetector;
$this->deviceView = $deviceView;
$this->router = $router;
Expand Down Expand Up @@ -113,6 +114,7 @@ public function handleRequest(GetResponseEvent $event)
// Sets the flag for the response handled by the GET switch param and the type of the view.
if ($this->deviceView->hasSwitchParam()) {
$event->setResponse($this->getRedirectResponseBySwitchParam($request));

return;
}

Expand All @@ -133,6 +135,7 @@ public function handleRequest(GetResponseEvent $event)
if (($response = $this->getRedirectResponse($request, $this->deviceView->getViewType()))) {
$event->setResponse($response);
}

return;
}

Expand Down Expand Up @@ -164,6 +167,8 @@ public function needsResponseModification()
* Handles the Response
*
* @param FilterResponseEvent $event
*
* @return null
*/
public function handleResponse(FilterResponseEvent $event)
{
Expand All @@ -179,14 +184,13 @@ public function handleResponse(FilterResponseEvent $event)
* Do we have to redirect?
*
* @param Request $request
* @param string $view For which view should be check?
* @param string $view For which view should be check?
*
* @return boolean
*/
protected function mustRedirect(Request $request, $view)
{
if (
!isset($this->redirectConf[$view]) ||
if (!isset($this->redirectConf[$view]) ||
!$this->redirectConf[$view]['is_enabled'] ||
($this->getRoutingOption($request->get('_route'), $view) === self::NO_REDIRECT)
) {
Expand All @@ -211,7 +215,7 @@ protected function mustRedirect(Request $request, $view)
*/
protected function prepareResponseModification($view)
{
$this->modifyResponseClosure = function(DeviceView $deviceView, FilterResponseEvent $event) use ($view) {
$this->modifyResponseClosure = function (DeviceView $deviceView, FilterResponseEvent $event) use ($view) {
return $deviceView->modifyResponse($view, $event->getResponse());
};
}
Expand All @@ -221,7 +225,7 @@ protected function prepareResponseModification($view)
*
* @param Request $request
*
* @return \SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie
* @return RedirectResponseWithCookie
*/
protected function getRedirectResponseBySwitchParam(Request $request)
{
Expand All @@ -236,8 +240,8 @@ protected function getRedirectResponseBySwitchParam(Request $request)
if (array_key_exists($this->deviceView->getSwitchParam(), $queryParams)) {
unset($queryParams[$this->deviceView->getSwitchParam()]);
}
if(sizeof($queryParams) > 0) {
$redirectUrl .= '?'. Request::normalizeQueryString(http_build_query($queryParams, null, '&'));
if (sizeof($queryParams) > 0) {
$redirectUrl .= '?'.Request::normalizeQueryString(http_build_query($queryParams, null, '&'));
}
} else {
$redirectUrl = $this->getCurrentHost($request);
Expand All @@ -251,9 +255,9 @@ protected function getRedirectResponseBySwitchParam(Request $request)
* Gets the RedirectResponse for the specified view.
*
* @param Request $request
* @param string $view The view for which we want the RedirectResponse.
* @param string $view The view for which we want the RedirectResponse.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @return RedirectResponse|null
*/
protected function getRedirectResponse(Request $request, $view)
{
Expand All @@ -272,9 +276,9 @@ protected function getRedirectResponse(Request $request, $view)
* Gets the redirect url.
*
* @param Request $request
* @param string $platform
* @param string $platform
*
* @return string
* @return string|null
*/
protected function getRedirectUrl(Request $request, $platform)
{
Expand All @@ -286,16 +290,12 @@ protected function getRedirectUrl(Request $request, $platform)
$queryParams = $request->query->all();
$queryParams[$this->deviceView->getSwitchParam()] = $platform;

return rtrim($this->redirectConf[$platform]['host'], '/') .
$request->getPathInfo() . '?' .
Request::normalizeQueryString(http_build_query($queryParams, null, '&'));
return rtrim($this->redirectConf[$platform]['host'], '/').$request->getPathInfo().'?'.Request::normalizeQueryString(http_build_query($queryParams, null, '&'));
} elseif (self::REDIRECT_WITHOUT_PATH === $routingOption) {
// Make sure to hint at the device override, otherwise infinite loop
// redirections may occur if different device views are hosted on
// different domains (since the cookie can't be shared across domains)
return $this->redirectConf[$platform]['host'] . '?' .
$this->deviceView->getSwitchParam() . '=' .
$platform;
return $this->redirectConf[$platform]['host'].'?'.$this->deviceView->getSwitchParam().'='.$platform;
} else {
return null;
}
Expand Down Expand Up @@ -341,7 +341,6 @@ protected function getRoutingOption($routeName, $optionName)
*/
protected function getCurrentHost(Request $request)
{
return $request->getScheme() . '://' . $request->getHost();
return $request->getScheme().'://'.$request->getHost();
}

}
27 changes: 12 additions & 15 deletions Helper/DeviceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@

namespace SunCat\MobileDetectBundle\Helper;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie;
use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector;

/**
* DeviceView
Expand All @@ -36,7 +33,7 @@ class DeviceView
const SWITCH_PARAM_DEFAULT = 'device_view';

/**
* @var \Symfony\Component\HttpFoundation\Request
* @var Request
*/
protected $request;

Expand Down Expand Up @@ -68,7 +65,7 @@ class DeviceView
/**
* Constructor
*
* @param \Symfony\Component\DependencyInjection\Container $serviceContainer
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack = null)
{
Expand Down Expand Up @@ -202,7 +199,7 @@ public function setNotMobileView()
/**
* Gets the switch param value from the query string (GET header).
*
* @return string
* @return string|null
*/
public function getSwitchParamValue()
{
Expand All @@ -218,7 +215,7 @@ public function getSwitchParamValue()
*
* @param string $redirectUrl
*
* @return \SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie
* @return RedirectResponseWithCookie
*/
public function getRedirectResponseBySwitchParam($redirectUrl)
{
Expand All @@ -237,10 +234,10 @@ public function getRedirectResponseBySwitchParam($redirectUrl)
/**
* Modifies the Response for the specified device view.
*
* @param string $view The device view for which the response should be modified.
* @param \Symfony\Component\HttpFoundation\Response $response
* @param string $view The device view for which the response should be modified.
* @param Response $response
*
* @return \Symfony\Component\HttpFoundation\Response
* @return Response
*/
public function modifyResponse($view, Response $response)
{
Expand All @@ -256,7 +253,7 @@ public function modifyResponse($view, Response $response)
* @param string $host Uri host
* @param int $statusCode Status code
*
* @return \SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie
* @return RedirectResponseWithCookie
*/
public function getRedirectResponse($view, $host, $statusCode)
{
Expand All @@ -266,7 +263,7 @@ public function getRedirectResponse($view, $host, $statusCode)
/**
* Setter of CookieKey
*
* @return string
* @param string $cookieKey
*/
public function setCookieKey($cookieKey)
{
Expand All @@ -286,7 +283,7 @@ public function getCookieKey()
/**
* Setter of SwitchParam
*
* @return string
* @param string $switchParam
*/
public function setSwitchParam($switchParam)
{
Expand Down Expand Up @@ -322,9 +319,9 @@ public function getCookieExpireDatetimeModifier()
/**
* Create the Cookie object
*
* @param string $cookieValue
* @param string $value
*
* @return \Symfony\Component\HttpFoundation\Cookie
* @return Cookie
*/
protected function createCookie($value)
{
Expand Down
4 changes: 2 additions & 2 deletions Twig/Extension/MobileDetectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
class MobileDetectExtension extends \Twig_Extension
{
/**
* @var \SunCat\MobileDetectBundle\DeviceDetector\MobileDetector
* @var MobileDetector
*/
private $mobileDetector;

/**
* @var \SunCat\MobileDetectBundle\Helper\DeviceView
* @var DeviceView
*/
private $deviceView;

Expand Down

0 comments on commit 4e10425

Please sign in to comment.