Completed
Pull Request — master (#176)
by Alejandro
04:03
created

ProcessVisitsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
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\Service\IpLocationResolverInterface;
8
use Shlinkio\Shlink\Core\Entity\VisitLocation;
9
use Shlinkio\Shlink\Core\Service\VisitServiceInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class ProcessVisitsCommand extends Command
17
{
18
    private const LOCALHOST = '127.0.0.1';
19
    public const NAME = 'visit:process';
20
21
    /**
22
     * @var VisitServiceInterface
23
     */
24
    private $visitService;
25
    /**
26
     * @var IpLocationResolverInterface
27
     */
28
    private $ipLocationResolver;
29
    /**
30
     * @var TranslatorInterface
31
     */
32
    private $translator;
33
34 2
    public function __construct(
35
        VisitServiceInterface $visitService,
36
        IpLocationResolverInterface $ipLocationResolver,
37
        TranslatorInterface $translator
38
    ) {
39 2
        $this->visitService = $visitService;
40 2
        $this->ipLocationResolver = $ipLocationResolver;
41 2
        $this->translator = $translator;
42 2
        parent::__construct(null);
43 2
    }
44
45 2
    public function configure()
46
    {
47 2
        $this->setName(self::NAME)
48 2
             ->setDescription(
49 2
                 $this->translator->translate('Processes visits where location is not set yet')
50
             );
51 2
    }
52
53 2
    public function execute(InputInterface $input, OutputInterface $output)
54
    {
55 2
        $io = new SymfonyStyle($input, $output);
56 2
        $visits = $this->visitService->getUnlocatedVisits();
57
58 2
        $count = 0;
59 2
        foreach ($visits as $visit) {
60 2
            $ipAddr = $visit->getRemoteAddr();
61 2
            $io->write(\sprintf('%s <info>%s</info>', $this->translator->translate('Processing IP'), $ipAddr));
62 2
            if ($ipAddr === self::LOCALHOST) {
63 1
                $io->writeln(
64 1
                    \sprintf(' (<comment>%s</comment>)', $this->translator->translate('Ignored localhost address'))
65
                );
66 1
                continue;
67
            }
68
69 2
            $count++;
70
            try {
71 2
                $result = $this->ipLocationResolver->resolveIpLocation($ipAddr);
72
73 2
                $location = new VisitLocation();
74 2
                $location->exchangeArray($result);
75 2
                $visit->setVisitLocation($location);
76 2
                $this->visitService->saveVisit($visit);
77
78 2
                $io->writeln(\sprintf(
79 2
                    ' (' . $this->translator->translate('Address located at "%s"') . ')',
80 2
                    $location->getCityName()
81
                ));
82
            } catch (WrongIpException $e) {
83
                $io->writeln(
84
                    \sprintf(' <error>%s</error>', $this->translator->translate('An error occurred while locating IP'))
85
                );
86
                if ($io->isVerbose()) {
87
                    $this->getApplication()->renderException($e, $output);
88
                }
89
            }
90
91 2
            if ($count === $this->ipLocationResolver->getApiLimit()) {
92
                $count = 0;
93
                $seconds = $this->ipLocationResolver->getApiInterval();
94
                $io->note(\sprintf(
95
                    $this->translator->translate('IP location resolver limit reached. Waiting %s seconds...'),
96
                    $seconds
97
                ));
98 2
                \sleep($seconds);
99
            }
100
        }
101
102 2
        $io->success($this->translator->translate('Finished processing all IPs'));
103 2
    }
104
}
105