|
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() |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
|
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.