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

DownloadDatabaseCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 4
dl 0
loc 63
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 17 1
A execute() 0 22 3
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\Command;
13
14
use GpsLab\Bundle\GeoIP2Bundle\Downloader\Downloader;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
class DownloadDatabaseCommand extends Command
22
{
23
    /**
24
     * @var Downloader
25
     */
26
    private $downloader;
27
28
    /**
29
     * @param Downloader $downloader
30
     */
31 5
    public function __construct(Downloader $downloader)
32
    {
33 5
        $this->downloader = $downloader;
34 5
        parent::__construct();
35 5
    }
36
37 5
    protected function configure(): void
38
    {
39
        $this
40 5
            ->setName('geoip2:download')
41 5
            ->setDescription('Downloads the GeoIP2 database')
42 5
            ->addArgument(
43 5
                'url',
44 5
                InputArgument::REQUIRED,
45 5
                'URL of downloaded GeoIP2 database'
46
            )
47 5
            ->addArgument(
48 5
                'target',
49 5
                InputArgument::REQUIRED,
50 5
                'Target download path'
51
            )
52
        ;
53 5
    }
54
55
    /**
56
     * @param InputInterface  $input
57
     * @param OutputInterface $output
58
     *
59
     * @return int
60
     */
61 5
    protected function execute(InputInterface $input, OutputInterface $output): int
62
    {
63 5
        $io = new SymfonyStyle($input, $output);
64 5
        $url = $input->getArgument('url');
65 5
        $target = $input->getArgument('target');
66
67 5
        if (!is_string($url)) {
68 2
            throw new \InvalidArgumentException(sprintf('URL of downloaded GeoIP2 database should be a string, got %s instead.', json_encode($url)));
69
        }
70
71 3
        if (!is_string($target)) {
72 2
            throw new \InvalidArgumentException(sprintf('Target download path should be a string, got %s instead.', json_encode($target)));
73
        }
74
75 1
        $io->title('Download the GeoIP2 database');
76
77 1
        $this->downloader->download($url, $target);
78
79 1
        $io->success('Finished downloading');
80
81 1
        return 0;
82
    }
83
}
84