1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\NavigationBundle\Command; |
4
|
|
|
|
5
|
|
|
use DH\NavigationBundle\Model\DistanceMatrix\Element; |
6
|
|
|
use DH\NavigationBundle\NavigationManager; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class DistanceMatrixCommand extends Command |
15
|
|
|
{ |
16
|
|
|
protected static $defaultName = 'navigation:distance-matrix'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var NavigationManager |
20
|
|
|
*/ |
21
|
|
|
private $manager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param NavigationManager $manager |
25
|
|
|
*/ |
26
|
|
|
public function __construct(NavigationManager $manager) |
27
|
|
|
{ |
28
|
|
|
$this->manager = $manager; |
29
|
|
|
|
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName('navigation:distance-matrix') |
40
|
|
|
->setDescription('Computes a distance matrix') |
41
|
|
|
->addOption('provider', null, InputOption::VALUE_REQUIRED) |
42
|
|
|
->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Origin') |
43
|
|
|
->addOption('to', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Destination') |
44
|
|
|
->setHelp(<<<'EOF' |
45
|
|
|
The <info>navigation:distance-matrix</info> command will compute a distance matrix from the given addresses. |
46
|
|
|
|
47
|
|
|
You can choose a provider with the "provider" option. |
48
|
|
|
|
49
|
|
|
<info>php bin/console navigation:distance-matrix --from="45.834278,1.260816" --to="44.830109,-0.603649" --provider=here</info> |
50
|
|
|
EOF |
51
|
|
|
) |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
if (empty($input->getOption('from'))) { |
61
|
|
|
throw new InvalidArgumentException('Mission required "from" option.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (empty($input->getOption('to'))) { |
65
|
|
|
throw new InvalidArgumentException('Mission required "to" option.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($input->getOption('provider')) { |
69
|
|
|
$this->manager->using($input->getOption('provider')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$query = $this->manager->createDistanceMatrixQuery(); |
73
|
|
|
|
74
|
|
|
foreach ($input->getOption('from') as $from) { |
75
|
|
|
$query->addOrigin($from); |
76
|
|
|
} |
77
|
|
|
foreach ($input->getOption('to') as $to) { |
78
|
|
|
$query->addDestination($to); |
79
|
|
|
} |
80
|
|
|
$response = $query->execute(); |
81
|
|
|
|
82
|
|
|
$origins = $query->getOrigins(); |
83
|
|
|
$destinations = $query->getDestinations(); |
84
|
|
|
|
85
|
|
|
$headers = array_merge([''], $destinations); |
86
|
|
|
$data = []; |
87
|
|
|
foreach ($response->getRows() as $index => $row) { |
88
|
|
|
$r = [$origins[$index]]; |
89
|
|
|
foreach ($row->getElements() as $element) { |
90
|
|
|
if (Element::STATUS_OK === $element->getStatus()) { |
91
|
|
|
$r[] = $element->getDistance().', '.$element->getDuration(); |
92
|
|
|
} else { |
93
|
|
|
$r[] = 'unavailable'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
$data[] = $r; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$table = new Table($output); |
100
|
|
|
$table |
101
|
|
|
->setHeaders($headers) |
102
|
|
|
->setRows($data) |
103
|
|
|
; |
104
|
|
|
$table->render(); |
105
|
|
|
|
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|