MigrationSqliteToRedisCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A execute() 0 32 4
1
<?php
2
3
namespace EtherpadLite\Console\Command;
4
5
use Exception;
6
use Predis\Client;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class MigrationSqliteToRedisCommand extends Command
14
{
15
    protected function configure()
16
    {
17
        $this->setName('redis:import:sqlite')
18
            ->setDescription('Import a sqlite database to redis')
19
            ->setDefinition(
20
                array(
21
                    new InputArgument('file', InputArgument::REQUIRED, 'The sqlite file'),
22
                    new InputOption('host', 'H', InputOption::VALUE_OPTIONAL, 'Redis hostname', 'localhost'),
23
                    new InputOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Redis port', 6379),
24
                )
25
            )
26
        ;
27
    }
28
29
    /**
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     * @return int|void
33
     * @throws Exception
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $file = $input->getArgument('file');
38
39
        if (!is_file($file)) {
40
            throw new Exception(sprintf('File %s not found!', $file));
41
        }
42
43
        $db = new \PDO(sprintf('sqlite:%s', $file));
44
        $redis = new Client(array(
45
            'scheme' => 'tcp',
46
            'host'   => $input->getOption('host'),
47
            'port'   => $input->getOption('port'),
48
        ));
49
50
        $c = 0;
51
52
        foreach ($db->query("SELECT * FROM store") as $row) {
53
            $parts = explode(':', $row['key']);
54
55
            $key = sprintf('ueberDB:keys:%s', $parts[0]);
56
            $value = sprintf('%s:%s', $parts[0], $parts[1]);
57
            $redis->sadd($key, $value);
58
59
            $redis->set($row['key'], $row['value']);
60
            $c++;
61
        }
62
63
        if ($output->isVerbose()) {
64
            $output->writeln(sprintf('%s values imported', $c));
65
        }
66
    }
67
}
68