Completed
Push — master ( fdba2f...3b8c9c )
by Peter
15s queued 11s
created

MaxMindDownloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 87
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B download() 0 60 5
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]);
0 ignored issues
show
Documentation introduced by
array($tmp_zip, $tmp_unzip, $tmp_untar) is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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]);
0 ignored issues
show
Documentation introduced by
array($tmp_zip, $tmp_unzip, $tmp_untar) is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
113 1
        $this->logger->debug(sprintf('Database moved to %s', $target));
114 1
    }
115
}
116