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

DownloadDatasetCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 24

Duplication

Lines 18
Ratio 58.06 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 18
loc 31
ccs 0
cts 31
cp 0
rs 8.5806
cc 4
eloc 24
nc 4
nop 2
crap 20
1
<?php
2
namespace Laposte\DatanovaBundle\Command;
3
4
use Laposte\DatanovaBundle\Service\Downloader;
5
use Symfony\Component\Console\Command\Command;
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
 * @author Florian Ajir <[email protected]>
13
 */
14
class DownloadDatasetCommand extends Command
15
{
16
    /** @var Downloader $downloader */
17
    private $downloader;
18
19
    /**
20
     * @param Downloader $downloader
21
     */
22
    public function __construct(Downloader $downloader)
23
    {
24
        $this->downloader = $downloader;
25
        parent::__construct();
26
    }
27
28 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...
29
    {
30
        $this
31
            ->setName('datanova:download:dataset')
32
            ->setDescription('Download dataset records to use it locally')
33
            ->addArgument(
34
                'dataset',
35
                InputArgument::REQUIRED,
36
                'Which dataset to download?'
37
            )
38
            ->addArgument(
39
                'format',
40
                InputArgument::OPTIONAL,
41
                'Data file format : CSV (default), JSON',
42
                'CSV'
43
            )
44
            ->addArgument(
45
                'q',
46
                InputArgument::OPTIONAL,
47
                'query filter, by default all results will be download'
48
            )
49
            ->addOption(
50
                'force-replace',
51
                'f',
52
                InputOption::VALUE_NONE,
53
                'If set, the command will replace local storage'
54
            );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $dataset = $input->getArgument('dataset');
63
        $format = strtolower($input->getArgument('format'));
64
        $query = $input->getArgument('q');
65
        $download = $this->downloader->download(
66
            $dataset,
67
            $format,
68
            $input->getArgument('q'),
69
            $input->getOption('force-replace')
70
        );
71
        $filepath = $this->downloader->findDownload($dataset, $format, $query);
72 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...
73
            $output->writeln(sprintf(
74
                'Dataset %s downloaded to "%s" : %d bytes',
75
                $dataset,
76
                $filepath,
77
                filesize($filepath)
78
            ));
79
        } else {
80
            if (false !== $filepath) {
81
                if (false === $input->getOption('force-replace')) {
82
                    $output->writeln('Existing dataset. To overwrite it, try with --force-replace option');
83
                } else {
84
                    $output->writeln('Error during update of existing dataset.');
85
                }
86
            } else {
87
                $output->writeln('Error during dataset download.');
88
            }
89
        }
90
    }
91
}
92