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

UpdateDatabaseCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 105
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 39 3
B execute() 0 35 8
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 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
            foreach (array_keys($this->databases) as $i => $name) {
56 1
                $databases_help .= sprintf(' * <info>%s</info>'.PHP_EOL, $name);
57
            }
58 1
            [$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 10
            ->setName('geoip2:update')
74 10
            ->setDescription('Update the GeoIP2 databases')
75 10
            ->addArgument(
76 10
                'databases',
77 10
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
78 10
                'Updated databases',
79 10
                array_keys($this->databases)
80
            )
81 10
            ->setHelp($help);
82 10
    }
83
84
    /**
85
     * @param InputInterface  $input
86
     * @param OutputInterface $output
87
     *
88
     * @return int
89
     */
90 10
    protected function execute(InputInterface $input, OutputInterface $output): int
91
    {
92 10
        $io = new SymfonyStyle($input, $output);
93 10
        $databases = $input->getArgument('databases');
94
95 10
        $io->title('Update the GeoIP2 databases');
96
97 10
        if (!is_array($databases)) {
98 2
            throw new \InvalidArgumentException(sprintf('Updated databases should be a array, got %s instead.', json_encode($databases)));
99
        }
100
101 8
        foreach ($databases as $database) {
102 8
            if (!array_key_exists($database, $this->databases)) {
103 2
                throw new \InvalidArgumentException(sprintf('Undefined "%s" database.', $database));
104
            }
105
106 6
            if (!array_key_exists('url', $this->databases[$database]) ||
107 5
                !array_key_exists('path', $this->databases[$database]) ||
108 3
                !is_string($this->databases[$database]['url']) ||
109 6
                !is_string($this->databases[$database]['path'])
110
            ) {
111 4
                throw new \InvalidArgumentException(sprintf('Invalid "%s" database config.', $database));
112
            }
113
114 2
            $io->section(sprintf('Update "%s" database', $database));
115
116 2
            $this->downloader->download($this->databases[$database]['url'], $this->databases[$database]['path']);
117
118 2
            $io->comment(sprintf('Database <info>%s</info> updated', $database));
119
        }
120
121 2
        $io->success('Finished updating');
122
123 2
        return 0;
124
    }
125
}
126