File src/nasa/src/NeoBundle/Command/ImportVariableDaysCommand.php copied from file src/nasa/src/NeoBundle/Command/ImportLastThreeDaysCommand.php (similarity 54%) (mode: 100644) (index c1bd9aa..52c4423) |
... |
... |
use Symfony\Component\Console\Input\InputArgument; |
10 |
10 |
use NeoBundle\Service\Importer; |
use NeoBundle\Service\Importer; |
11 |
11 |
use NeoBundle\Exception\NeoImportStructureInvalidException; |
use NeoBundle\Exception\NeoImportStructureInvalidException; |
12 |
12 |
|
|
13 |
|
class ImportLastThreeDaysCommand extends ContainerAwareCommand |
|
|
13 |
|
class ImportVariableDaysCommand extends ContainerAwareCommand |
14 |
14 |
{ |
{ |
|
15 |
|
const ARGUMENT_TOTAL_DAYS = 'total-days'; |
|
16 |
|
|
15 |
17 |
protected function configure() |
protected function configure() |
16 |
18 |
{ |
{ |
17 |
19 |
$this |
$this |
18 |
|
->setName('neo:import:lastThreeDays') |
|
19 |
|
->setDescription('Imports last 3 days form nasa api') |
|
|
20 |
|
->setName('neo:import:variableDays') |
|
21 |
|
->setDescription('Imports variable days form nasa api') |
|
22 |
|
->addArgument(self::ARGUMENT_TOTAL_DAYS, InputArgument::REQUIRED, 'Total days to fetch in the past') |
20 |
23 |
; |
; |
21 |
24 |
} |
} |
22 |
25 |
|
|
23 |
26 |
protected function execute(InputInterface $input, OutputInterface $output) |
protected function execute(InputInterface $input, OutputInterface $output) |
24 |
27 |
{ |
{ |
25 |
|
$output->writeln("<info>Import last 3 days from nasa api...</info>"); |
|
|
28 |
|
$totalDays = $input->getArgument(self::ARGUMENT_TOTAL_DAYS); |
|
29 |
|
|
|
30 |
|
$output->writeln("<info>Import variable days from nasa api...</info>"); |
26 |
31 |
$output->writeln(""); |
$output->writeln(""); |
27 |
32 |
|
|
28 |
33 |
/** @var NeoBundle\Service\Importer $neoImporter */ |
/** @var NeoBundle\Service\Importer $neoImporter */ |
29 |
34 |
$neoImporter = $this->getContainer()->get('neo.import'); |
$neoImporter = $this->getContainer()->get('neo.import'); |
30 |
35 |
|
|
31 |
36 |
try { |
try { |
32 |
|
$neoImporter->import(); |
|
|
37 |
|
$end = new \DateTime(); |
|
38 |
|
$start = new \DateTime(); |
|
39 |
|
$start->sub(new \DateInterval('P' . 1 . 'D')); |
|
40 |
|
|
|
41 |
|
for ($i = 0; $i < $totalDays; $i++) { |
|
42 |
|
$neoImporter->import($start, $end); |
|
43 |
|
$start->sub(new \DateInterval('P' . 2 . 'D')); |
|
44 |
|
$end->sub(new \DateInterval('P' . 2 . 'D')); |
|
45 |
|
} |
33 |
46 |
|
|
34 |
47 |
$output->writeln("<info>Import complete!</info>"); |
$output->writeln("<info>Import complete!</info>"); |
35 |
48 |
} catch (NeoImportStructureInvalidException $e) { |
} catch (NeoImportStructureInvalidException $e) { |
|
... |
... |
class ImportLastThreeDaysCommand extends ContainerAwareCommand |
38 |
51 |
$output->writeln("<error>Import failed: invalid url!</error>"); |
$output->writeln("<error>Import failed: invalid url!</error>"); |
39 |
52 |
} |
} |
40 |
53 |
} |
} |
41 |
|
} |
|
|
54 |
|
} |
File src/nasa/src/NeoBundle/Service/Import.php changed (mode: 100644) (index 8e62372..5aa1ebb) |
... |
... |
class Import |
54 |
54 |
$this->days = $days; |
$this->days = $days; |
55 |
55 |
} |
} |
56 |
56 |
|
|
57 |
|
public function import() |
|
|
57 |
|
public function import(\DateTime $start = null, \DateTime $end = null) |
58 |
58 |
{ |
{ |
59 |
|
$result = json_decode($this->buzz->get($this->getUrl())->getContent(), true); |
|
|
59 |
|
if ($start === null || $end === null) { |
|
60 |
|
$end = new \DateTime(); |
|
61 |
|
$start = new \DateTime(); |
|
62 |
|
$start->sub(new \DateInterval('P' . ($this->days - 1) . 'D')); |
|
63 |
|
} |
|
64 |
|
|
|
65 |
|
$result = json_decode($this->buzz->get($this->getUrl($start, $end))->getContent(), true); |
60 |
66 |
|
|
61 |
67 |
if ($this->analyze($result)) { |
if ($this->analyze($result)) { |
62 |
68 |
$this->process($result['near_earth_objects']); |
$this->process($result['near_earth_objects']); |
|
... |
... |
class Import |
109 |
115 |
} |
} |
110 |
116 |
} |
} |
111 |
117 |
|
|
112 |
|
private function getUrl() |
|
|
118 |
|
private function getUrl(\DateTime $start, \DateTime $end) |
113 |
119 |
{ |
{ |
114 |
|
$end = new \DateTime(); |
|
115 |
|
$start = new \DateTime(); |
|
116 |
|
$start->sub(new \DateInterval('P' . ($this->days - 1) . 'D')); |
|
117 |
|
|
|
118 |
120 |
return str_replace( |
return str_replace( |
119 |
121 |
'##key##', |
'##key##', |
120 |
122 |
$this->key, |
$this->key, |