|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Visit; |
|
3
|
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
|
5
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException; |
|
6
|
|
|
use Shlinkio\Shlink\Common\Service\IpLocationResolver; |
|
7
|
|
|
use Shlinkio\Shlink\Common\Service\IpLocationResolverInterface; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService; |
|
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 Zend\I18n\Translator\TranslatorInterface; |
|
15
|
|
|
|
|
16
|
|
|
class ProcessVisitsCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
const LOCALHOST = '127.0.0.1'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var VisitServiceInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $visitService; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var IpLocationResolverInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $ipLocationResolver; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var TranslatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $translator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ProcessVisitsCommand constructor. |
|
35
|
|
|
* @param VisitServiceInterface $visitService |
|
36
|
|
|
* @param IpLocationResolverInterface $ipLocationResolver |
|
37
|
|
|
* @param TranslatorInterface $translator |
|
38
|
|
|
* |
|
39
|
|
|
* @Inject({VisitService::class, IpLocationResolver::class, "translator"}) |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function __construct( |
|
42
|
|
|
VisitServiceInterface $visitService, |
|
43
|
|
|
IpLocationResolverInterface $ipLocationResolver, |
|
44
|
|
|
TranslatorInterface $translator |
|
45
|
|
|
) { |
|
46
|
2 |
|
$this->visitService = $visitService; |
|
47
|
2 |
|
$this->ipLocationResolver = $ipLocationResolver; |
|
48
|
2 |
|
$this->translator = $translator; |
|
49
|
2 |
|
parent::__construct(null); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function configure() |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->setName('visit:process') |
|
55
|
2 |
|
->setDescription( |
|
56
|
2 |
|
$this->translator->translate('Processes visits where location is not set yet') |
|
57
|
|
|
); |
|
58
|
2 |
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$visits = $this->visitService->getUnlocatedVisits(); |
|
63
|
|
|
|
|
64
|
2 |
|
foreach ($visits as $visit) { |
|
65
|
2 |
|
$ipAddr = $visit->getRemoteAddr(); |
|
66
|
2 |
|
$output->write(sprintf('%s <info>%s</info>', $this->translator->translate('Processing IP'), $ipAddr)); |
|
67
|
2 |
|
if ($ipAddr === self::LOCALHOST) { |
|
68
|
1 |
|
$output->writeln( |
|
69
|
1 |
|
sprintf(' (<comment>%s</comment>)', $this->translator->translate('Ignored localhost address')) |
|
70
|
|
|
); |
|
71
|
1 |
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
2 |
|
$result = $this->ipLocationResolver->resolveIpLocation($ipAddr); |
|
76
|
2 |
|
$location = new VisitLocation(); |
|
77
|
2 |
|
$location->exchangeArray($result); |
|
78
|
2 |
|
$visit->setVisitLocation($location); |
|
79
|
2 |
|
$this->visitService->saveVisit($visit); |
|
80
|
2 |
|
$output->writeln(sprintf( |
|
81
|
2 |
|
' (' . $this->translator->translate('Address located at "%s"') . ')', |
|
82
|
2 |
|
$location->getCityName() |
|
83
|
|
|
)); |
|
84
|
|
|
} catch (WrongIpException $e) { |
|
85
|
2 |
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$output->writeln($this->translator->translate('Finished processing all IPs')); |
|
90
|
2 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|