Skip to content

Commit

Permalink
Refactor geolocatorAbstract class, improve data filtering method
Browse files Browse the repository at this point in the history
  • Loading branch information
Xorg committed Dec 14, 2023
1 parent 6e4b49e commit 3c973f0
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions src/EventSubscriber/GeoLocatorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,34 @@ public function __construct( GeolocatorFactory $geolocatorFactory )
*/
public function onKernelRequest(RequestEvent $event): void
{
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
// don't do anything if it's not the master request
return;
}

$nameRoute = $event->getRequest()->get('_route');
if (!$this->containsKeyword($nameRoute, ['profile', '_wd'])) {
if (!$this->geolocatorFactory->getGeolocatorService()->checkIpPing()) {
throw new TooManyRequestsHttpException(3600,'Too Many request. |-> BANNIS.');
}
}

}

public function onKernelController(ControllerArgumentsEvent $event): void
{
// Early return if it's not the master request
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
list($request, $controller, $redirectRequired, $nameRoute) = $this->handleRequest($event);

// Early return if it's not the master request | return if the controller is a profiler controller or a redirect is required
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType() || $this->isProfilerController($controller) || $redirectRequired) {
return;
}

$request = $event->getRequest();
$controller = $request->attributes?->get('_controller');
$redirectRequired = $request->server?->get('REDIRECT_URL') === "/unauthorized";

// Early return if the controller is a profiler controller or a redirect is required
if ($this->isProfilerController($controller) || $redirectRequired) {
return;
}

$nameRoute = $request->get('_route');
$excludedRoutes = ['profile', '_wd'];
if (!$this->geolocatorFactory->getGeolocatorService()->checkIpPing()) {
throw new TooManyRequestsHttpException(3600,'Too Many request. |-> BANNIS.');
}
}

public function onKernelController(ControllerArgumentsEvent $event): void
{

list($request, $controller, $redirectRequired, $nameRoute) = $this->handleRequest($event);

// Early return if the nameRoute doesn't contain any of the excluded keywords
if ($this->containsKeyword($nameRoute, $excludedRoutes)) {
// Early return if it's not the master request | return if the controller is a profiler controller or a redirect is required
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType() || $this->isProfilerController($controller) || $redirectRequired) {
return;
}

$geolocator = $this->geolocatorFactory->getGeolocatorService()->checkAuthorize();

// Early return if no redirection is required
Expand All @@ -92,17 +82,19 @@ private function isProfilerController($controller): bool
return str_starts_with($controller, 'web_profiler.controller.profiler::');
}

private function containsKeyword($haystack, array $keywords): bool
/**
* @param ControllerArgumentsEvent | RequestEvent $event
*
* @return array
*/
public function handleRequest(RequestEvent|ControllerArgumentsEvent $event): array
{
return array_reduce($keywords, static function (bool $carry, string $keyword) use ($haystack): bool {
return $carry || strpos($haystack, $keyword) !== false; // Check if keyword in string
}, false) && array_reduce($keywords, static function (bool $carry, string $keyword) use ($haystack): bool {
return $carry || strpos($haystack, $keyword) === 0; // Check if keyword at start of string
}, false);
$request = $event->getRequest();
$nameRoute = $request->get('_route');
$controller = $request->attributes?->get('_controller');
$redirectRequired = $request->server?->get('REDIRECT_URL') === "/unauthorized";

// return array_reduce($keywords, static function (bool $carry, string $keyword) use ($haystack): bool {
// return $carry || stripos($haystack, $keyword) !== false;
// }, false);
return array($request, $controller, $redirectRequired, $nameRoute);
}

public static function getSubscribedEvents(): array
Expand Down

0 comments on commit 3c973f0

Please sign in to comment.