Completed
Push — master ( 64470f...3a69fd )
by Florian
02:24
created

DownloadDatasetLegacyCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 28
loc 28
ccs 0
cts 28
cp 0
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
crap 2
1
<?php
2
namespace Laposte\DatanovaBundle\Command;
3
4
use Laposte\DatanovaBundle\Service\Downloader;
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Symfony 2.3 ContainerAwareCommand version of DownloadDatasetCommand
13
 * @author Florian Ajir <[email protected]>
14
 */
15
class DownloadDatasetLegacyCommand extends ContainerAwareCommand
16
{
17
    /** @var Downloader $downloader */
18
    private $downloader;
19
20
    /**
21
     * Command configuration
22
     */
23 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $this
26
            ->setName('datanova:legacy:download:dataset')
27
            ->setDescription('Download dataset records to use it locally')
28
            ->addArgument(
29
                'dataset',
30
                InputArgument::REQUIRED,
31
                'Which dataset to download?'
32
            )
33
            ->addArgument(
34
                'format',
35
                InputArgument::OPTIONAL,
36
                'Data file format : CSV (default), JSON',
37
                'CSV'
38
            )
39
            ->addArgument(
40
                'q',
41
                InputArgument::OPTIONAL,
42
                'query filter, by default all results will be download'
43
            )
44
            ->addOption(
45
                'force-replace',
46
                'f',
47
                InputOption::VALUE_NONE,
48
                'If set, the command will replace local storage'
49
            );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $this->downloader = $this->getContainer()->get('data_nova.service.downloader');
58
        $dataset = $input->getArgument('dataset');
59
        $format = strtolower($input->getArgument('format'));
60
        $query = $input->getArgument('q');
61
        $download = $this->downloader->download(
62
            $dataset,
63
            $format,
64
            $input->getArgument('q'),
65
            $input->getOption('force-replace')
66
        );
67
        $filepath = $this->downloader->findDownload($dataset, $format, $query);
68 View Code Duplication
        if ($download) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            $output->writeln(sprintf(
70
                'Dataset %s downloaded to "%s" : %d bytes',
71
                $dataset,
72
                $filepath,
73
                filesize($filepath)
74
            ));
75
        } else {
76
            if (false !== $filepath) {
77
                if (false === $input->getOption('force-replace')) {
78
                    $output->writeln('Existing dataset. To overwrite it, try with --force-replace option');
79
                } else {
80
                    $output->writeln('Error during update of existing dataset.');
81
                }
82
            } else {
83
                $output->writeln('Error during dataset download.');
84
            }
85
        }
86
    }
87
}
88