ImportCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 8.5806
cc 4
eloc 17
nc 5
nop 2
crap 4
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\Output\OutputInterface;
8
9
/**
10
 * Class ImportCommand
11
 *
12
 * @package Velikonja\LabbyBundle\Command
13
 * @author  Matej Velikonja <[email protected]>
14
 */
15
class ImportCommand extends BaseCommand
16
{
17
    const COMMAND_NAME = 'labby:database:import';
18
19
    /**
20
     * Configure command.
21
     */
22 10
    protected function configure()
23
    {
24
        $this
25 10
            ->setRoles(array(self::ROLE_LOCAL))
26 10
            ->setName(self::COMMAND_NAME)
27 10
            ->setDescription('Import SQL dump to local database.')
28 10
            ->addArgument('file', InputArgument::REQUIRED, 'Location of SQL dump (compressed or uncompressed).');
29 10
    }
30
31
    /**
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     *
35
     * @return int|null|void
36
     */
37 3
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39 3
        $filePath = $input->getArgument('file');
40 3
        $tempFile = false;
41 3
        $zip      = $this->getContainer()->get('velikonja_labby.util.zip_archive');
42
43 3
        if (!file_exists($filePath)) {
44 1
            throw new \InvalidArgumentException(
45
                sprintf(
46 1
                    'File `%s` does not exists.',
47
                    $filePath
48
                )
49
            );
50
        }
51
52 2
        if ($zip->isZipped($filePath)) {
53 1
            $filePath = $zip->unzip($filePath);
54 1
            $tempFile = true;
55
        }
56
57 2
        $importer = $this->getContainer()->get('velikonja_labby.service.db.importer');
58
59 2
        $importer->import($filePath);
60
61
        // cleanup if we unzip archive
62 2
        if ($tempFile) {
63 1
            unlink($filePath);
64
        }
65
66 2
        $output->writeln('<info>Database successfully imported!</info>');
67 2
    }
68
}
69