Test Failed
Push — master ( 0c6ae7...5a133e )
by Yaroslav
02:43
created

DownloadFilesCommand   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 84.13%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 146
ccs 53
cts 63
cp 0.8413
rs 10
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadGeneralFile() 0 5 1
A getFiles() 0 20 4
A postalCodeFile() 0 3 1
A downloadPostalCodeFile() 0 5 1
A getPostalFiles() 0 16 5
A downloadFile() 0 22 3
A handle() 0 13 3
A extractFile() 0 13 4
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 4
    public function handle()
33
    {
34 4
        foreach ($this->getFiles() as $file) {
35 4
            $this->downloadGeneralFile($file);
36 4
            $this->extractFile($file);
37
        }
38
39 4
        foreach ($this->getPostalFiles() as $file) {
40 4
            $this->downloadPostalCodeFile($file);
41 4
            $this->extractFile($this->postalCodeFile($file));
42
        }
43
44 4
        return 0;
45
    }
46
47
    /**
48
     * Get default files.
49
     *
50
     * @return array
51
     */
52 4
    protected function getFiles(): array
53
    {
54 4
        $files = $this->argument('files');
55 4
        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 4
        if (!is_array($files)) {
68
            throw new \InvalidArgumentException('"files" argument should be valid array.');
69
        }
70
71 4
        return $files;
72
    }
73
74
    /**
75
     * Get default files.
76
     *
77
     * @return array
78
     */
79 4
    protected function getPostalFiles(): array
80
    {
81 4
        $files = $this->option('postal');
82 4
        if (is_array($files)) {
0 ignored issues
show
introduced by
The condition is_array($files) is always true.
Loading history...
83 4
            $files = array_filter($files);
84
        }
85 4
        if (empty($files) && $this->option('defaults')) {
86
            $files = [
87
                'allCountries.zip',
88
            ];
89
        }
90 4
        if (!is_array($files)) {
91
            throw new \InvalidArgumentException('"postal" option should be valid array.');
92
        }
93
94 4
        return $files;
95
    }
96
97 4
    protected function downloadGeneralFile(string $file)
98
    {
99 4
        $baseUrl = rtrim(config('geonames.import_repo'), '/') . '/';
100 4
        $url     = $baseUrl . $file;
101 4
        $this->downloadFile($file, $url);
102 4
    }
103
104 4
    protected function downloadPostalCodeFile(string $file)
105
    {
106 4
        $baseUrl = rtrim(config('geonames.import_postal_code_repo'), '/') . '/';
107 4
        $url     = $baseUrl . $file;
108 4
        $this->downloadFile($this->postalCodeFile($file), $url);
109 4
    }
110
111 4
    protected function downloadFile(string $file, string $url)
112
    {
113 4
        $this->info("Download: {$url} to {$file}");
114 4
        $bar = $this->output->createProgressBar(10000);
115 4
        $bar->start();
116 4
        $result = GeoDataImporter::storeFileFromUrl(
0 ignored issues
show
Bug introduced by
The method storeFileFromUrl() does not exist on LaraGeoData\Facades\GeoDataImporter. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        /** @scrutinizer ignore-call */ 
117
        $result = GeoDataImporter::storeFileFromUrl(
Loading history...
117 4
            $url,
118
            $file,
119 4
            function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($bar) {
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

119
            function ($resource, $download_size, $downloaded, /** @scrutinizer ignore-unused */ $upload_size, $uploaded) use ($bar) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
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 ignore-unused  annotation

119
            function ($resource, $download_size, $downloaded, $upload_size, /** @scrutinizer ignore-unused */ $uploaded) use ($bar) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
                if ($download_size > 0) {
121
                    $percent = round($downloaded / $download_size * 10000);
122
                    $bar->setProgress($percent);
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

122
                    $bar->setProgress(/** @scrutinizer ignore-type */ $percent);
Loading history...
123
                }
124
                sleep(1);
125 4
            },
126 4
            (bool) $this->option('force')
127
        );
128 4
        $bar->finish();
129 4
        if ($result) {
130 3
            $this->info(' Downloaded.');
131
        } else {
132 2
            $this->error(' Skipped.');
133
        }
134 4
    }
135
136 4
    protected function postalCodeFile(string $file): string
137
    {
138 4
        return rtrim(config('geonames.storage.postal_codes_dir'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
139
    }
140
141
142 4
    protected function extractFile(string $file)
143
    {
144 4
        if (!$this->option('extract')) {
145 3
            return;
146
        }
147 1
        if (!Str::endsWith($file, '.zip')) {
148
            return;
149
        }
150 1
        $result = GeoDataImporter::extractFile($file);
0 ignored issues
show
Bug introduced by
The method extractFile() does not exist on LaraGeoData\Facades\GeoDataImporter. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        /** @scrutinizer ignore-call */ 
151
        $result = GeoDataImporter::extractFile($file);
Loading history...
151 1
        if ($result) {
152 1
            $this->info("Extracted file: {$file}");
153
        } else {
154
            $this->error("Error extraction file: {$file}");
155
        }
156 1
    }
157
}
158