-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeoIpCommand.php
More file actions
150 lines (128 loc) · 4.66 KB
/
Copy pathGeoIpCommand.php
File metadata and controls
150 lines (128 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace Dot\GeoIP\Command;
use Dot\GeoIP\Client;
use Dot\GeoIP\Extractor;
use Dot\GeoIP\FileSystem;
use Dot\GeoIP\Service\LocationService;
use Dot\GeoIP\Service\LocationServiceInterface;
use Exception;
use InvalidArgumentException;
use MaxMind\Db\Reader\Metadata;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function array_key_exists;
use function array_keys;
use function date;
use function implode;
use function ini_set;
use function preg_match;
use function sprintf;
#[AsCommand(
name: 'geoip:synchronize',
description: 'Download latest version of GeoLite2-* database files.',
)]
class GeoIpCommand extends Command
{
/** @var string $defaultName */
public static $defaultName = 'geoip:synchronize';
private FileSystem $fileSystem;
public function __construct(
protected LocationServiceInterface $locationService,
) {
parent::__construct(self::$defaultName);
$this->fileSystem = new FileSystem();
}
public function configure(): void
{
$this
->setName(self::$defaultName)
->setDescription('Download latest version of GeoLite2-* database files.')
->addOption(
'database',
'd',
InputOption::VALUE_OPTIONAL,
sprintf(
'Database name. Accepted values: %s',
implode(', ', array_keys(LocationService::DATABASES))
),
LocationService::DATABASE_ALL
)
->addOption(
'memory-limit',
'm',
InputOption::VALUE_OPTIONAL,
'Memory limit for decompression. Examples: 256M, 1G',
'128M'
);
}
/**
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$memoryLimit = $input->getOption('memory-limit');
$this->validateMemoryLimit($memoryLimit);
ini_set('memory_limit', $memoryLimit);
$this->fileSystem->mkdir($this->locationService->getConfig('targetDir'));
$database = $input->getOption('database') ?? LocationService::DATABASE_ALL;
$databases = $this->identifyDatabases($database);
foreach ($databases as $database) {
$tempFilePath = $this->locationService->getTempFilePath($database);
$realFilePath = $this->locationService->getRealFilePath($database);
$oldVersion = 'n/a';
$oldMetadata = $this->locationService->getDatabaseMetadata($database);
if ($oldMetadata instanceof Metadata) {
$oldVersion = date('Y-m-d H:i:s', $oldMetadata->buildEpoch);
}
(new Client())->get($this->locationService->getDatabaseSourceUrl($database), $tempFilePath);
$this->fileSystem->remove($realFilePath);
(new Extractor())->extract($tempFilePath, $realFilePath);
$this->fileSystem->remove($tempFilePath);
$newVersion = 'n/a';
$newMetadata = $this->locationService->getDatabaseMetadata($database);
if ($newMetadata instanceof Metadata) {
$newVersion = date('Y-m-d H:i:s', $newMetadata->buildEpoch);
}
$output->writeln(sprintf("%s: %s -> %s", $database, $oldVersion, $newVersion));
}
return self::SUCCESS;
}
/**
* @throws Exception
*/
public function identifyDatabases(string $identifier): array
{
if ($identifier === LocationService::DATABASE_ALL) {
return [
LocationService::DATABASE_ASN,
LocationService::DATABASE_CITY,
LocationService::DATABASE_COUNTRY,
];
}
if (! array_key_exists($identifier, LocationService::DATABASES)) {
$message = sprintf(
'Invalid database identifier: %s. Use one of the following identifiers: %s.',
$identifier,
implode(', ', array_keys(LocationService::DATABASES))
);
throw new Exception($message);
}
return [$identifier];
}
/**
* @throws InvalidArgumentException
*/
private function validateMemoryLimit(string $memoryLimit): void
{
if (preg_match('/^(\d+)([MG])$/', $memoryLimit) === 1) {
return;
}
throw new InvalidArgumentException(
sprintf('Invalid memory limit: %s', $memoryLimit)
);
}
}