visitService = $visitService; $this->ipLocationResolver = $ipLocationResolver; $this->translator = $translator; parent::__construct(null); } protected function configure(): void { $this->setName(self::NAME) ->setDescription( $this->translator->translate('Processes visits where location is not set yet') ); } protected function execute(InputInterface $input, OutputInterface $output): void { $io = new SymfonyStyle($input, $output); $visits = $this->visitService->getUnlocatedVisits(); $count = 0; foreach ($visits as $visit) { $ipAddr = $visit->getRemoteAddr(); $io->write(sprintf('%s %s', $this->translator->translate('Processing IP'), $ipAddr)); if ($ipAddr === IpAddress::LOCALHOST) { $io->writeln( sprintf(' (%s)', $this->translator->translate('Ignored localhost address')) ); continue; } $count++; try { $result = $this->ipLocationResolver->resolveIpLocation($ipAddr); $location = new VisitLocation(); $location->exchangeArray($result); $visit->setVisitLocation($location); $this->visitService->saveVisit($visit); $io->writeln(sprintf( ' (' . $this->translator->translate('Address located at "%s"') . ')', $location->getCityName() )); } catch (WrongIpException $e) { $io->writeln( sprintf(' %s', $this->translator->translate('An error occurred while locating IP')) ); if ($io->isVerbose()) { $this->getApplication()->renderException($e, $output); } } if ($count === $this->ipLocationResolver->getApiLimit()) { $count = 0; $seconds = $this->ipLocationResolver->getApiInterval(); $io->note(sprintf( $this->translator->translate('IP location resolver limit reached. Waiting %s seconds...'), $seconds )); sleep($seconds); } } $io->success($this->translator->translate('Finished processing all IPs')); } }