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

UpdateDatabaseCommand::configure()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 12
cts 16
cp 0.75
rs 9.296
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3.1406
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[]
30
     */
31
    private $databases;
32
33
    /**
34
     * @param Downloader $downloader
35
     * @param array[]    $databases
36
     */
37 8
    public function __construct(Downloader $downloader, array $databases)
38
    {
39 8
        $this->downloader = $downloader;
40 8
        $this->databases = $databases;
41 8
        parent::__construct();
42 8
    }
43
44 8
    protected function configure(): void
45
    {
46
        $help = <<<'EOT'
47 8
The <info>%command.name%</info> command update all configured databases:
48
49
    <info>%command.full_name%</info>
50
51
EOT;
52
53 8
        if (count($this->databases) >= 2) {
54
            $databases_help = '';
55
            foreach (array_keys($this->databases) as $i => $name) {
56
                $databases_help .= sprintf(' * <info>%s</info>'.PHP_EOL, $name);
57
            }
58
            [$first, $second, ] = array_keys($this->databases);
0 ignored issues
show
Bug introduced by
The variable $first does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $second does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
59
60
            $help .= <<<'EOT'
61
62
Update the <info>$first</info> and <info>$second</info> database:
63
64
    <info>%command.full_name% $first $second</info>
65
66
List of available databases:
67
68
$databases_help
69
EOT;
70
        }
71
72
        $this
73 8
            ->setName('geoip2:update')
74 8
            ->setDescription('Update the GeoIP2 databases')
75 8
            ->addArgument(
76 8
                'databases',
77 8
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
78 8
                'Updated databases',
79 8
                array_keys($this->databases)
80
            )
81 8
            ->setHelp($help);
82 8
    }
83
84
    /**
85
     * @param InputInterface  $input
86
     * @param OutputInterface $output
87
     *
88
     * @return int
89
     */
90 8
    protected function execute(InputInterface $input, OutputInterface $output): int
91
    {
92 8
        $io = new SymfonyStyle($input, $output);
93
        $databases = $input->getArgument('databases');
94
95
        $io->title('Update the GeoIP2 databases');
96
97
        if (!is_array($databases)) {
98
            throw new \InvalidArgumentException(sprintf('Updated databases should be a array, got %s instead.', json_encode($databases)));
99
        }
100
101
        foreach ($databases as $database) {
102
            if (!array_key_exists($database, $this->databases)) {
103
                throw new \InvalidArgumentException(sprintf('Undefined "%s" database.', $database));
104
            }
105
106
            if (!array_key_exists('url', $this->databases[$database]) ||
107
                !array_key_exists('path', $this->databases[$database]) ||
108
                !is_string($this->databases[$database]['url']) ||
109
                !is_string($this->databases[$database]['path'])
110
            ) {
111
                throw new \InvalidArgumentException(sprintf('Invalid "%s" database config.', $database));
112
            }
113
114
            $io->section(sprintf('Update "%s" database', $database));
115
116
            $this->downloader->download($this->databases[$database]['url'], $this->databases[$database]['path']);
117
118
            $io->comment(sprintf('Database <info>%s</info> updated', $database));
119
        }
120
121
        $io->success('Finished updating');
122
123
        return 0;
124
    }
125
}
126