Passed
Pull Request — master (#73)
by Peter
02:15
created

MaxMindDownloader::download()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 5

Importance

Changes 4
Bugs 2 Features 1
Metric Value
cc 5
eloc 34
nc 6
nop 2
dl 0
loc 59
ccs 35
cts 35
cp 1
crap 5
rs 9.0648
c 4
b 2
f 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2017, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Bundle\GeoIP2Bundle\Downloader;
13
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\Filesystem\Filesystem;
16
17
/**
18
 * MaxMind downloader.
19
 *
20
 * Expect GeoIP2 archive from https://download.maxmind.com/ with structure:
21
 *
22
 * GeoLite2-City.tar.gz
23
 *  - GeoLite2-City_20200114
24
 *    - COPYRIGHT.txt
25
 *    - GeoLite2-City.mmdb
26
 *    - LICENSE.txt
27
 *    - README.txt
28
 */
29
class MaxMindDownloader implements Downloader
30
{
31
    /**
32
     * @var Filesystem
33
     */
34
    private $fs;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41
    /**
42
     * @param Filesystem      $fs
43
     * @param LoggerInterface $logger
44
     */
45 2
    public function __construct(Filesystem $fs, LoggerInterface $logger)
46
    {
47 2
        $this->fs = $fs;
48 2
        $this->logger = $logger;
49 2
    }
50
51
    /**
52
     * @param string $url
53
     * @param string $target
54
     */
55 2
    public function download(string $url, string $target): void
56
    {
57 2
        $id = uniqid('', true);
58 2
        $tmp_zip = sprintf('%s/%s_GeoLite2.tar.gz', sys_get_temp_dir(), $id);
59 2
        $tmp_unzip = sprintf('%s/%s_GeoLite2.tar', sys_get_temp_dir(), $id);
60 2
        $tmp_untar = sprintf('%s/%s_GeoLite2', sys_get_temp_dir(), $id);
61
62
        // remove old files and folders for correct overwrite it
63 2
        $this->fs->remove([$tmp_zip, $tmp_unzip, $tmp_untar]);
64
65 2
        $this->logger->debug(sprintf('Beginning download of file %s', $url));
66
67 2
        $this->fs->copy($url, $tmp_zip, true);
68
69 2
        $this->logger->debug(sprintf('Download complete to %s', $tmp_zip));
70 2
        $this->logger->debug(sprintf('De-compressing file to %s', $tmp_unzip));
71
72 2
        $this->fs->mkdir(dirname($target), 0755);
73
74
        // decompress gz file
75 2
        $zip = new \PharData($tmp_zip);
76 2
        $tar = $zip->decompress();
77
78 2
        $this->logger->debug('Decompression complete');
79 2
        $this->logger->debug(sprintf('Extract tar file to %s', $tmp_untar));
80
81
        // extract tar archive
82 2
        $tar->extractTo($tmp_untar);
83
84 2
        $this->logger->debug('Tar archive extracted');
85
86
        // find database in archive
87 2
        $database = '';
88 2
        $files = glob(sprintf('%s/**/*.mmdb', $tmp_untar)) ?: [];
89 2
        foreach ($files as $file) {
90
            // expected something like that "GeoLite2-City_20200114"
91 1
            if (preg_match('/(?<database>[^\/]+)_(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})/', $file, $match)) {
92 1
                $this->logger->debug(sprintf(
93 1
                    'Found %s database updated at %s-%s-%s in %s',
94 1
                    $match['database'],
95 1
                    $match['year'],
96 1
                    $match['month'],
97 1
                    $match['day'],
98 1
                    $file
99
                ));
100
            }
101
102 1
            $database = $file;
103
        }
104
105 2
        if (!$database) {
106 1
            throw new \RuntimeException('Not found GeoLite2 database in archive.');
107
        }
108
109 1
        $this->fs->copy($database, $target, true);
110 1
        $this->fs->chmod($target, 0755);
111 1
        $this->fs->remove([$tmp_zip, $tmp_unzip, $tmp_untar]);
112
113 1
        $this->logger->debug(sprintf('Database moved to %s', $target));
114 1
    }
115
}
116