Completed
Pull Request — master (#278)
by Alejandro
03:36 queued 01:25
created

UpdateDbCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 49
ccs 21
cts 24
cp 0.875
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 23 3
A configure() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Command\Visit;
5
6
use Shlinkio\Shlink\Common\Exception\RuntimeException;
7
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class UpdateDbCommand extends Command
15
{
16
    public const NAME = 'visit:update-db';
17
18
    /**
19
     * @var DbUpdaterInterface
20
     */
21
    private $geoLiteDbUpdater;
22
23 2
    public function __construct(DbUpdaterInterface $geoLiteDbUpdater)
24
    {
25 2
        parent::__construct();
26 2
        $this->geoLiteDbUpdater = $geoLiteDbUpdater;
27
    }
28
29 2
    protected function configure(): void
30
    {
31
        $this
32 2
            ->setName(self::NAME)
33 2
            ->setDescription('Updates the GeoLite2 database file used to geolocate IP addresses')
34 2
            ->setHelp(
35
                'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run '
36 2
                . 'every first Wednesday'
37
            );
38
    }
39
40 2
    protected function execute(InputInterface $input, OutputInterface $output): void
41
    {
42 2
        $io = new SymfonyStyle($input, $output);
43 2
        $progressBar = new ProgressBar($output);
44 2
        $progressBar->start();
45
46
        try {
47
            $this->geoLiteDbUpdater->downloadFreshCopy(function (int $total, int $downloaded) use ($progressBar) {
48
                $progressBar->setMaxSteps($total);
49
                $progressBar->setProgress($downloaded);
50 2
            });
51
52 1
            $progressBar->finish();
53 1
            $io->writeln('');
54
55 1
            $io->success('GeoLite2 database properly updated');
56 1
        } catch (RuntimeException $e) {
57 1
            $progressBar->finish();
58 1
            $io->writeln('');
59
60 1
            $io->error('An error occurred while updating GeoLite2 database');
61 1
            if ($io->isVerbose()) {
62
                $this->getApplication()->renderException($e, $output);
63
            }
64
        }
65
    }
66
}
67