1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Visit; |
5
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\Common\Exception\RuntimeException; |
7
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
14
|
|
|
|
15
|
|
|
class UpdateDbCommand extends Command |
16
|
|
|
{ |
17
|
|
|
public const NAME = 'visit:update-db'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var DbUpdaterInterface |
21
|
|
|
*/ |
22
|
|
|
private $geoLiteDbUpdater; |
23
|
|
|
/** |
24
|
|
|
* @var TranslatorInterface |
25
|
|
|
*/ |
26
|
|
|
private $translator; |
27
|
|
|
|
28
|
2 |
|
public function __construct(DbUpdaterInterface $geoLiteDbUpdater, TranslatorInterface $translator) |
29
|
|
|
{ |
30
|
2 |
|
$this->geoLiteDbUpdater = $geoLiteDbUpdater; |
31
|
2 |
|
$this->translator = $translator; |
32
|
2 |
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
protected function configure(): void |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
2 |
|
->setName(self::NAME) |
39
|
2 |
|
->setDescription( |
40
|
2 |
|
$this->translator->translate('Updates the GeoLite2 database file used to geolocate IP addresses') |
41
|
|
|
) |
42
|
2 |
|
->setHelp($this->translator->translate( |
43
|
|
|
'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run ' |
44
|
2 |
|
. 'every first Wednesday' |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output): void |
49
|
|
|
{ |
50
|
2 |
|
$io = new SymfonyStyle($input, $output); |
51
|
2 |
|
$progressBar = new ProgressBar($output); |
52
|
2 |
|
$progressBar->start(); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$this->geoLiteDbUpdater->downloadFreshCopy(function (int $total, int $downloaded) use ($progressBar) { |
56
|
|
|
$progressBar->setMaxSteps($total); |
57
|
|
|
$progressBar->setProgress($downloaded); |
58
|
2 |
|
}); |
59
|
|
|
|
60
|
1 |
|
$progressBar->finish(); |
61
|
1 |
|
$io->writeln(''); |
62
|
|
|
|
63
|
1 |
|
$io->success($this->translator->translate('GeoLite2 database properly updated')); |
64
|
1 |
|
} catch (RuntimeException $e) { |
65
|
1 |
|
$progressBar->finish(); |
66
|
1 |
|
$io->writeln(''); |
67
|
|
|
|
68
|
1 |
|
$io->error($this->translator->translate('An error occurred while updating GeoLite2 database')); |
69
|
1 |
|
if ($io->isVerbose()) { |
70
|
|
|
$this->getApplication()->renderException($e, $output); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|