DumpCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Velikonja\LabbyBundle\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class DumpCommand
12
 *
13
 * @package Velikonja\LabbyBundle\Command
14
 * @author  Matej Velikonja <[email protected]>
15
 */
16
class DumpCommand extends BaseCommand
17
{
18
    const COMMAND_NAME    = 'labby:database:dump';
19
    const ARG_CONFIG_PATH = 'config-path';
20
21
    /**
22
     * Configure command.
23
     */
24 10
    protected function configure()
25
    {
26
        $this
27 10
            ->setRoles(array(self::ROLE_REMOTE))
28 10
            ->setName(self::COMMAND_NAME)
29 10
            ->setDescription('Dump local database.')
30 10
            ->addArgument('file', InputArgument::OPTIONAL, 'File path to write to.')
31 10
            ->addOption(
32 10
                'compress',
33 10
                'c',
34 10
                InputOption::VALUE_NONE,
35 10
                'Compress file. Works only if file argument is given.'
36
            );
37 10
    }
38
39
    /**
40
     * @param InputInterface  $input
41
     * @param OutputInterface $output
42
     *
43
     * @return int|null|void
44
     */
45 3
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47 3
        $filePath = $input->getArgument('file');
48 3
        $compress = $input->getOption('compress');
49 3
        $dumper   = $this->getContainer()->get('velikonja_labby.service.db.dumper');
50 3
        $zip      = $this->getContainer()->get('velikonja_labby.util.zip_archive');
51
52 3
        $dump = $dumper->dump($output);
53
54 3
        if ($filePath) {
55 2
            if ($compress) {
56 1
                $zip->zip($filePath, $dump);
57
            } else {
58 1
                file_put_contents($filePath, $dump);
59
            }
60 2
            $output->writeln(sprintf('<info>Database successfully dumped to `%s`!</info>', realpath($filePath)));
61
        } else {
62 1
            $output->writeln($dump);
63
        }
64
65 3
    }
66
}
67