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

DownloadDatasetCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 58.97 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 46
loc 78
wmc 6
lcom 1
cbo 4
ccs 0
cts 64
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B configure() 28 28 1
B execute() 18 31 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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