1 | <?php |
||||||
2 | |||||||
3 | namespace LaraGeoData\Console\Commands; |
||||||
4 | |||||||
5 | use Illuminate\Console\Command; |
||||||
6 | use Illuminate\Support\Str; |
||||||
7 | use LaraGeoData\Facades\GeoDataImporter; |
||||||
8 | |||||||
9 | class DownloadFilesCommand extends Command |
||||||
10 | { |
||||||
11 | /** |
||||||
12 | * The name and signature of the console command. |
||||||
13 | * |
||||||
14 | * @var string |
||||||
15 | */ |
||||||
16 | protected $signature = 'geonames:download |
||||||
17 | {files?* : Files to download. } |
||||||
18 | {--postal=* : Postal files to download. } |
||||||
19 | {--force : Override files any if exists. } |
||||||
20 | {--extract : Extract archive. } |
||||||
21 | {--defaults : ODownload default files set. } |
||||||
22 | '; |
||||||
23 | |||||||
24 | /** |
||||||
25 | * The console command description. |
||||||
26 | * |
||||||
27 | * @var string |
||||||
28 | */ |
||||||
29 | protected $description = 'Download and extract files from server.'; |
||||||
30 | |||||||
31 | |||||||
32 | 8 | public function handle() |
|||||
33 | { |
||||||
34 | 8 | foreach ($this->getFiles() as $file) { |
|||||
35 | 6 | $this->downloadGeneralFile($file); |
|||||
36 | 6 | $this->extractFile($file); |
|||||
37 | } |
||||||
38 | |||||||
39 | 8 | foreach ($this->getPostalFiles() as $file) { |
|||||
40 | 6 | $this->downloadPostalCodeFile($file); |
|||||
41 | 6 | $this->extractFile($this->postalCodeFile($file)); |
|||||
42 | } |
||||||
43 | |||||||
44 | 8 | return 0; |
|||||
45 | } |
||||||
46 | |||||||
47 | /** |
||||||
48 | * Get default files. |
||||||
49 | * |
||||||
50 | * @return array |
||||||
51 | */ |
||||||
52 | 8 | protected function getFiles(): array |
|||||
53 | { |
||||||
54 | 8 | $files = $this->argument('files'); |
|||||
55 | 8 | if (empty($files) && $this->option('defaults')) { |
|||||
56 | $files = [ |
||||||
57 | 'allCountries.zip', |
||||||
58 | 'alternateNames.zip', |
||||||
59 | 'hierarchy.zip', |
||||||
60 | 'admin1CodesASCII.txt', |
||||||
61 | 'admin2Codes.txt', |
||||||
62 | 'featureCodes_en.txt', |
||||||
63 | 'timeZones.txt', |
||||||
64 | 'countryInfo.txt', |
||||||
65 | ]; |
||||||
66 | } |
||||||
67 | 8 | if (!is_array($files)) { |
|||||
68 | throw new \InvalidArgumentException('"files" argument should be valid array.'); |
||||||
69 | } |
||||||
70 | |||||||
71 | 8 | return $files; |
|||||
72 | } |
||||||
73 | |||||||
74 | /** |
||||||
75 | * Get default files. |
||||||
76 | * |
||||||
77 | * @return array |
||||||
78 | */ |
||||||
79 | 8 | protected function getPostalFiles(): array |
|||||
80 | { |
||||||
81 | 8 | $files = $this->option('postal'); |
|||||
82 | 8 | if (is_array($files)) { |
|||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
83 | 8 | $files = array_filter($files); |
|||||
84 | } |
||||||
85 | 8 | if (empty($files) && $this->option('defaults')) { |
|||||
86 | $files = [ |
||||||
87 | 'allCountries.zip', |
||||||
88 | ]; |
||||||
89 | } |
||||||
90 | 8 | if (!is_array($files)) { |
|||||
91 | throw new \InvalidArgumentException('"postal" option should be valid array.'); |
||||||
92 | } |
||||||
93 | |||||||
94 | 8 | return $files; |
|||||
95 | } |
||||||
96 | |||||||
97 | 6 | protected function downloadGeneralFile(string $file) |
|||||
98 | { |
||||||
99 | 6 | $baseUrl = rtrim(config('geonames.import_repos.general'), '/') . '/'; |
|||||
100 | 6 | $url = $baseUrl . $file; |
|||||
101 | 6 | $this->downloadFile($file, $url); |
|||||
102 | 6 | } |
|||||
103 | |||||||
104 | 6 | protected function downloadPostalCodeFile(string $file) |
|||||
105 | { |
||||||
106 | 6 | $baseUrl = rtrim(config('geonames.import_repos.zip'), '/') . '/'; |
|||||
107 | 6 | $url = $baseUrl . $file; |
|||||
108 | 6 | $this->downloadFile($this->postalCodeFile($file), $url); |
|||||
109 | 6 | } |
|||||
110 | |||||||
111 | 8 | protected function downloadFile(string $file, string $url) |
|||||
112 | { |
||||||
113 | 8 | $this->info("Download: {$url} to {$file}"); |
|||||
114 | 8 | $bar = $this->output->createProgressBar(10000); |
|||||
115 | 8 | $bar->start(); |
|||||
116 | 8 | $result = GeoDataImporter::storageCreateFromUrl( |
|||||
117 | 8 | $url, |
|||||
118 | $file, |
||||||
119 | 8 | function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bar) { |
|||||
0 ignored issues
–
show
The parameter
$upload_size is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$uploaded is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
120 | if ($download_size > 0) { |
||||||
121 | $percent = round($downloaded / $download_size * 10000); |
||||||
122 | $bar->setProgress($percent); |
||||||
0 ignored issues
–
show
$percent of type double is incompatible with the type integer expected by parameter $step of Symfony\Component\Consol...gressBar::setProgress() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
123 | } |
||||||
124 | sleep(1); |
||||||
125 | 8 | }, |
|||||
126 | 8 | (bool) $this->option('force') |
|||||
127 | ); |
||||||
128 | 8 | $bar->finish(); |
|||||
129 | 8 | if ($result) { |
|||||
130 | 7 | $this->info(' Downloaded.'); |
|||||
131 | } else { |
||||||
132 | 2 | $this->error(' Skipped.'); |
|||||
133 | } |
||||||
134 | 8 | } |
|||||
135 | |||||||
136 | 6 | protected function postalCodeFile(string $file): string |
|||||
137 | { |
||||||
138 | 6 | return rtrim(config('geonames.storage.postal_codes_dir'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file; |
|||||
139 | } |
||||||
140 | |||||||
141 | |||||||
142 | 8 | protected function extractFile(string $file) |
|||||
143 | { |
||||||
144 | 8 | if (!$this->option('extract')) { |
|||||
145 | 3 | return; |
|||||
146 | } |
||||||
147 | 5 | if (!Str::endsWith($file, '.zip')) { |
|||||
148 | return; |
||||||
149 | } |
||||||
150 | 5 | $result = GeoDataImporter::storageExtractZipFile($file); |
|||||
151 | 5 | if ($result) { |
|||||
152 | 5 | $this->info("Extracted file: {$file}"); |
|||||
153 | } else { |
||||||
154 | $this->error("Error extraction file: {$file}"); |
||||||
155 | } |
||||||
156 | 5 | } |
|||||
157 | } |
||||||
158 |