UpdateDatabaseCommand::execute()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 8
Bugs 1 Features 1
Metric Value
cc 9
eloc 21
c 8
b 1
f 1
nc 6
nop 2
dl 0
loc 37
ccs 18
cts 18
cp 1
crap 9
rs 8.0555
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 UpdateDatabaseCommand extends Command
22
{
23
    /**
24
     * @var Downloader
25
     */
26
    private $downloader;
27
28
    /**
29
     * @var array<string, ?array{url?: string|false, path?: string|false, locales?: string[], license?: ?string, edition?: string}>
30
     */
31
    private $databases;
32
33
    /**
34
     * @param Downloader                                                                                                              $downloader
35
     * @param array<string, ?array{url?: string|false, path?: string|false, locales?: string[], license?: ?string, edition?: string}> $databases
36
     */
37 10
    public function __construct(Downloader $downloader, array $databases)
38
    {
39 10
        $this->downloader = $downloader;
40 10
        $this->databases = $databases;
41 10
        parent::__construct();
42 10
    }
43
44 10
    protected function configure(): void
45
    {
46
        $help = <<<'EOT'
47 10
The <info>%command.name%</info> command update all configured databases:
48
49
    <info>%command.full_name%</info>
50
51
EOT;
52
53 10
        if (count($this->databases) >= 2) {
54 1
            $databases_help = '';
55 1
56 1
            foreach (array_keys($this->databases) as $name) {
57
                $databases_help .= sprintf(' * <info>%s</info>'.PHP_EOL, $name);
58 1
            }
59
60
            [$first, $second] = array_keys($this->databases);
61
62
            $help .= <<<EOT
63
64
Update the <info>$first</info> and <info>$second</info> database:
65
66
    <info>%command.full_name% $first $second</info>
67
68
List of available databases:
69
70
$databases_help
71
EOT;
72
        }
73 10
74 10
        $this
75 10
            ->setName('geoip2:update')
76 10
            ->setDescription('Update the GeoIP2 databases')
77 10
            ->addArgument(
78 10
                'databases',
79 10
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
80
                'Updated databases',
81 10
                array_keys($this->databases)
82 10
            )
83
            ->setHelp($help);
84
    }
85
86
    /**
87
     * @param InputInterface  $input
88
     * @param OutputInterface $output
89
     *
90 10
     * @return int
91
     */
92 10
    protected function execute(InputInterface $input, OutputInterface $output): int
93 10
    {
94
        $io = new SymfonyStyle($input, $output);
95 10
        $databases = $input->getArgument('databases');
96
97 10
        $io->title('Update the GeoIP2 databases');
98 2
99
        if (!is_array($databases)) {
100
            throw new \InvalidArgumentException(sprintf('Updated databases should be a array, got %s instead.', json_encode($databases)));
101 8
        }
102 8
103 2
        foreach ($databases as $database) {
104
            if (!array_key_exists($database, $this->databases)) {
105
                throw new \InvalidArgumentException(sprintf('Undefined "%s" database.', $database));
106 6
            }
107 5
108 3
            if (!array_key_exists('url', $this->databases[$database])
109 6
                || !array_key_exists('path', $this->databases[$database])
110
                || !is_string($this->databases[$database]['url'])
111 4
                || !is_string($this->databases[$database]['path'])
112
            ) {
113
                throw new \InvalidArgumentException(sprintf('Invalid "%s" database config.', $database));
114 2
            }
115
116 2
            $io->section(sprintf('Update "%s" database', $database));
117
118 2
            if (empty($this->databases[$database]['license'])) {
119
                $io->warning(sprintf('GeoIP Database %s has no maxmind license key', $database));
120
            } else {
121 2
                $this->downloader->download($this->databases[$database]['url'], $this->databases[$database]['path']);
122
                $io->comment(sprintf('Database <info>%s</info> updated', $database));
123 2
            }
124
        }
125
126
        $io->success('Finished updating');
127
128
        return 0;
129
    }
130
}
131