DbDumpCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 81
ccs 0
cts 57
cp 0
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 50 9
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Command;
6
7
use App\Infra\Db\ContredanseDb;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputDefinition;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Process\Process;
14
15
class DbDumpCommand extends Command
16
{
17
    /**
18
     * @var ContredanseDb
19
     */
20
    private $db;
21
22
    public function __construct(ContredanseDb $db)
23
    {
24
        parent::__construct();
25
        $this->db = $db;
26
    }
27
28
    /**
29
     * Configures the command.
30
     */
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setName('db:dump')
35
            ->setDescription('Dump external database')
36
            ->setDefinition(
37
                new InputDefinition([
38
                    new InputOption('file', 'f', InputOption::VALUE_REQUIRED),
39
                ])
40
            );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        if (!$input->hasOption('file')) {
49
            throw new \Exception('Missing dir argument, use <command> <file>');
50
        }
51
        $dumpFile = $input->hasOption('file') ? $input->getOption('file') : '';
52
53
        if (!is_string($dumpFile) || !is_dir(dirname($dumpFile))) {
54
            throw new \Exception(sprintf(
55
                'Dump directory %s does not exists',
56
                is_string($dumpFile) ? dirname($dumpFile) : ''
57
            ));
58
        }
59
60
        $output->writeln('Starting dump, it\'s gonna take a while');
61
62
        $params = $this->db->getConnectionInfo();
63
64
        $command = [
65
            file_exists('./bin/mysqldump') ? './bin/mysqldump' : 'mysqldump',
66
            '--extended-insert',
67
            '--no-create-db',
68
            '--compress',
69
            sprintf('--user=%s', $params['username']),
70
            sprintf('--password=%s', $params['password']),
71
            sprintf('--host=%s', $params['host']),
72
            $params['dbname'],
73
            sprintf('> %s', $dumpFile)
74
        ];
75
76
        $process = Process::fromShellCommandline(implode(' ', $command));
77
        $process->setTimeout(7200);
78
        $process->setIdleTimeout(30);
79
80
        try {
81
            $process->mustRun(function ($type, $buffer): void {
82
                if (Process::ERR === $type) {
83
                    echo 'ERR > ' . $buffer;
84
                } else {
85
                    echo '.';
86
                }
87
            });
88
        } catch (\Throwable $e) {
89
            $msg = str_replace($params['password'], '******', $e->getMessage());
90
            throw new \RuntimeException($msg);
91
        }
92
93
        $output->writeln("\nDump done !");
94
95
        return 1;
96
    }
97
}
98