src/EventSubscriber/CityEventSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Info;
  4. use App\Entity\Region;
  5. use App\Manager\InfoManager;
  6. use App\Manager\IpApiManager;
  7. use App\Manager\LogIpApiManager;
  8. use App\Manager\RegionManager;
  9. use App\Service\LocationIP\Api;
  10. use App\Service\LocationIP\LocationIpInterface as LocationIP;
  11. use App\Service\Regionalization\CheckBot;
  12. use App\Service\Regionalization\CheckController;
  13. use App\Service\Regionalization\CheckParameter;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\String\Slugger\SluggerInterface;
  19. class CityEventSubscriber implements EventSubscriberInterface
  20. {
  21.     private RequestStack $requestStack;
  22.     private Api $locationIP;
  23.     private RegionManager $regionManager;
  24.     private InfoManager $infoManager;
  25.     private LogIpApiManager $logIpApiManager;
  26.     private IpApiManager $ipApiManager;
  27.     private CheckController $checkController;
  28.     private CheckParameter $checkParameter;
  29.     private CheckBot $checkBot;
  30.     private SluggerInterface $slugger;
  31.     public function __construct(RequestStack $requestStackApi $locationIPRegionManager $regionManagerInfoManager $infoManagerLogIpApiManager $logIpApiManagerIpApiManager $ipApiManagerCheckController $checkControllerCheckParameter $checkParameterCheckBot $checkBotSluggerInterface $slugger) {
  32.         $this->requestStack $requestStack;
  33.         $this->locationIP $locationIP;
  34.         $this->regionManager $regionManager;
  35.         $this->infoManager $infoManager;
  36.         $this->logIpApiManager $logIpApiManager;
  37.         $this->ipApiManager $ipApiManager;
  38.         $this->checkParameter $checkParameter;
  39.         $this->checkController $checkController;
  40.         $this->checkBot $checkBot;
  41.         $this->slugger $slugger;
  42.     }
  43.     public function onControllerEvent(ControllerEvent $event): void
  44.     {
  45.         return;
  46.         /** @var Info $info */
  47.         $info $this->infoManager->find(1);
  48.         if (!$info->isRegionInfoShow()) {
  49.             $this->clearRegionCitySession();
  50.             return;
  51.         }
  52.         $session $this->requestStack->getSession();
  53.         if ($session->get(LocationIP::CLEAR) === true or $session->get(LocationIP::INVALID) === true) {
  54.             $this->clearRegionCitySession();
  55.             return;
  56.         }
  57.         if (!is_null($this->getCitySession())) {
  58.             return;
  59.         }
  60.         $eventController $event->getController();
  61.         $controller $eventController;
  62.         $method null;
  63.         if (is_array($eventController)) {
  64.             $controller get_class($eventController[0]);
  65.             $method $eventController[1];
  66.         }
  67.         if (!is_string($controller) or !(is_string($method) or is_null($method))) {
  68.             $this->clearRegionCitySession();
  69.             return;
  70.         }
  71.         if (!$this->checkController->isValid($controller$method)) {
  72.             $this->clearRegionCitySession();
  73.             return;
  74.         }
  75.         $request $event->getRequest();
  76.         if (is_null($request)) {
  77.             $this->clearRegionCitySession();
  78.             return;
  79.         }
  80.         $parameters $request->query->all();
  81.         if (!$this->checkParameter->isValid($parameters)) {
  82.             $this->clearRegionCitySession();
  83.             return;
  84.         }
  85.         $userAgent $request->headers->get('User-Agent');
  86.         if (!$this->checkBot->isValid($userAgent)) {
  87.             $this->clearRegionCitySession();
  88.             return;
  89.         }
  90.         $cityName $this->getCityByRequest($request$controller$method);
  91.         if (is_null($cityName)) {
  92.             $this->clearRegionCitySession();
  93.             $session->set(LocationIP::INVALIDtrue);
  94.             return;
  95.         }
  96.         $slug $this->slugger->slug($cityName)->lower()->toString();
  97.         $cities $this->regionManager->listInMap('');
  98.         $cities array_filter($cities, function (Region $region) use ($slug) {
  99.             if ($region->getSlug() == $slug) {
  100.                 return true;
  101.             }
  102.             if (empty($values $region->apiValuesArray())) {
  103.                 return false;
  104.             }
  105.             $values array_map(function ($item) {
  106.                 return $this->slugger->slug($item)->lower()->toString();
  107.             }, $values);
  108.             return in_array($slug$values);
  109.         });
  110.         $cities array_values($cities);
  111.         /** @var Region $city */
  112.         $city $cities[0] ?? null;
  113.         if (is_null($city)) {
  114.             $this->clearRegionCitySession();
  115.             $session->set(LocationIP::INVALIDtrue);
  116.             return;
  117.         }
  118.         if ($city->getPropertiesActive()->count() < 1) {
  119.             $this->clearRegionCitySession();
  120.             $session->set(LocationIP::INVALIDtrue);
  121.             return;
  122.         }
  123.         $session->set(LocationIP::REGION$city);
  124.     }
  125.     public static function getSubscribedEvents(): array
  126.     {
  127.         return [
  128.             ControllerEvent::class => 'onControllerEvent',
  129.         ];
  130.     }
  131.     protected function getCitySession(): ?Region
  132.     {
  133.         $session $this->requestStack->getSession();
  134.         return $session->get(LocationIP::REGION);
  135.     }
  136.     protected function clearRegionCitySession(): void
  137.     {
  138.         $session $this->requestStack->getSession();
  139.         $session->remove(LocationIP::REGION);
  140.     }
  141.     protected function getCityByRequest(Request $requeststring $controller, ?string $method): ?string
  142.     {
  143.         $ip $request->getClientIp();
  144.         $ipApi $this->ipApiManager->last($ip);
  145.         if (!is_null($ipApi)) {
  146.             return $ipApi->getCity();
  147.         }
  148.         $ipApiResponse $this->locationIP->fromRequest($request);
  149.         $ipApiResponse->setController($controller);
  150.         $ipApiResponse->setMethod($method);
  151.         $this->logIpApiManager->save($ipApiResponse);
  152.         $city $ipApiResponse->getDepartment();
  153.         $ipApi $this->ipApiManager->byIp($ip);
  154.         if (is_null($ipApi)) {
  155.             $ipApi $this->ipApiManager->create();
  156.             $ipApi->setIp($ip);
  157.         }
  158.         $ipApi->setCity($city);
  159.         $this->ipApiManager->save($ipApi);
  160.         return $city;
  161.     }
  162. }