1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Visit; |
5
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException; |
7
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; |
8
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress; |
9
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation; |
10
|
|
|
use Shlinkio\Shlink\Core\Service\VisitServiceInterface; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
class ProcessVisitsCommand extends Command |
19
|
|
|
{ |
20
|
|
|
public const NAME = 'visit:process'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var VisitServiceInterface |
24
|
|
|
*/ |
25
|
|
|
private $visitService; |
26
|
|
|
/** |
27
|
|
|
* @var IpLocationResolverInterface |
28
|
|
|
*/ |
29
|
|
|
private $ipLocationResolver; |
30
|
|
|
/** |
31
|
|
|
* @var TranslatorInterface |
32
|
|
|
*/ |
33
|
|
|
private $translator; |
34
|
|
|
|
35
|
2 |
|
public function __construct( |
36
|
|
|
VisitServiceInterface $visitService, |
37
|
|
|
IpLocationResolverInterface $ipLocationResolver, |
38
|
|
|
TranslatorInterface $translator |
39
|
|
|
) { |
40
|
2 |
|
$this->visitService = $visitService; |
41
|
2 |
|
$this->ipLocationResolver = $ipLocationResolver; |
42
|
2 |
|
$this->translator = $translator; |
43
|
2 |
|
parent::__construct(); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
protected function configure(): void |
47
|
|
|
{ |
48
|
2 |
|
$this->setName(self::NAME) |
49
|
2 |
|
->setDescription( |
50
|
2 |
|
$this->translator->translate('Processes visits where location is not set yet') |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): void |
55
|
|
|
{ |
56
|
2 |
|
$io = new SymfonyStyle($input, $output); |
57
|
2 |
|
$visits = $this->visitService->getUnlocatedVisits(); |
58
|
|
|
|
59
|
2 |
|
foreach ($visits as $visit) { |
60
|
2 |
|
if (! $visit->hasRemoteAddr()) { |
61
|
1 |
|
$io->writeln( |
62
|
1 |
|
sprintf('<comment>%s</comment>', $this->translator->translate('Ignored visit with no IP address')), |
63
|
1 |
|
OutputInterface::VERBOSITY_VERBOSE |
64
|
|
|
); |
65
|
1 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$ipAddr = $visit->getRemoteAddr(); |
69
|
2 |
|
$io->write(sprintf('%s <fg=blue>%s</>', $this->translator->translate('Processing IP'), $ipAddr)); |
70
|
2 |
|
if ($ipAddr === IpAddress::LOCALHOST) { |
71
|
1 |
|
$io->writeln( |
72
|
1 |
|
sprintf(' [<comment>%s</comment>]', $this->translator->translate('Ignored localhost address')) |
73
|
|
|
); |
74
|
1 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
try { |
78
|
2 |
|
$result = $this->ipLocationResolver->resolveIpLocation($ipAddr); |
79
|
|
|
|
80
|
2 |
|
$location = new VisitLocation($result); |
81
|
2 |
|
$visit->setVisitLocation($location); |
82
|
2 |
|
$this->visitService->saveVisit($visit); |
83
|
|
|
|
84
|
2 |
|
$io->writeln(sprintf( |
85
|
2 |
|
' [<info>' . $this->translator->translate('Address located at "%s"') . '</info>]', |
86
|
2 |
|
$location->getCountryName() |
87
|
|
|
)); |
88
|
|
|
} catch (WrongIpException $e) { |
89
|
|
|
$io->writeln( |
90
|
|
|
sprintf( |
91
|
|
|
' [<fg=red>%s</>]', |
92
|
|
|
$this->translator->translate('An error occurred while locating IP. Skipped') |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
if ($io->isVerbose()) { |
96
|
2 |
|
$this->getApplication()->renderException($e, $output); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$io->success($this->translator->translate('Finished processing all IPs')); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|