Completed
Pull Request — master (#47)
by Peter
02:55
created

DownloadDatabaseCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.2077

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 2
cts 12
cp 0.1666
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 8.2077
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 4
    public function __construct(Downloader $downloader)
32
    {
33 4
        $this->downloader = $downloader;
34 4
        parent::__construct();
35 4
    }
36
37 4
    protected function configure(): void
38
    {
39
        $this
40 4
            ->setName('geoip2:download')
41 4
            ->setDescription('Downloads the GeoIP2 database')
42 4
            ->addArgument(
43 4
                'url',
44 4
                InputArgument::REQUIRED,
45 4
                'URL of downloaded GeoIP2 database'
46
            )
47 4
            ->addArgument(
48 4
                'target',
49 4
                InputArgument::REQUIRED,
50 4
                'Target download path'
51
            )
52
        ;
53 4
    }
54
55
    /**
56
     * @param InputInterface  $input
57
     * @param OutputInterface $output
58
     *
59
     * @return int
60
     */
61 4
    protected function execute(InputInterface $input, OutputInterface $output): int
62
    {
63 4
        $io = new SymfonyStyle($input, $output);
64
        $url = $input->getArgument('url');
65
        $target = $input->getArgument('target');
66
67
        if (!is_string($url)) {
68
            throw new \InvalidArgumentException(sprintf('URL of downloaded GeoIP2 database should be a string, got %s instead.', json_encode($url)));
69
        }
70
71
        if (!is_string($target)) {
72
            throw new \InvalidArgumentException(sprintf('Target download path should be a string, got %s instead.', json_encode($target)));
73
        }
74
75
        $io->title('Download the GeoIP2 database');
76
77
        $this->downloader->download($url, $target);
78
79
        $io->success('Finished downloading');
80
81
        return 0;
82
    }
83
}
84