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
|
|
|
public function __construct(NavigationManager $manager) |
24
|
|
|
{ |
25
|
|
|
$this->manager = $manager; |
26
|
|
|
|
27
|
|
|
parent::__construct(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function configure(): void |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->setName('navigation:distance-matrix') |
37
|
|
|
->setDescription('Computes a distance matrix') |
38
|
|
|
->addOption('provider', null, InputOption::VALUE_REQUIRED) |
39
|
|
|
->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Origin') |
40
|
|
|
->addOption('to', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Destination') |
41
|
|
|
->setHelp( |
42
|
|
|
<<<'EOF' |
43
|
|
|
The <info>navigation:distance-matrix</info> command will compute a distance matrix from the given addresses. |
44
|
|
|
|
45
|
|
|
You can choose a provider with the "provider" option. |
46
|
|
|
|
47
|
|
|
<info>php bin/console navigation:distance-matrix --from="45.834278,1.260816" --to="44.830109,-0.603649" --provider=here</info> |
48
|
|
|
EOF |
49
|
|
|
) |
50
|
|
|
; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
if (empty($input->getOption('from'))) { |
59
|
|
|
throw new InvalidArgumentException('Mission required "from" option.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (empty($input->getOption('to'))) { |
63
|
|
|
throw new InvalidArgumentException('Mission required "to" option.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($input->getOption('provider')) { |
67
|
|
|
$this->manager->using($input->getOption('provider')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$query = $this->manager->createDistanceMatrixQuery(); |
71
|
|
|
|
72
|
|
|
foreach ($input->getOption('from') as $from) { |
73
|
|
|
$query->addOrigin($from); |
74
|
|
|
} |
75
|
|
|
foreach ($input->getOption('to') as $to) { |
76
|
|
|
$query->addDestination($to); |
77
|
|
|
} |
78
|
|
|
$response = $query->execute(); |
79
|
|
|
|
80
|
|
|
$origins = $query->getOrigins(); |
81
|
|
|
$destinations = $query->getDestinations(); |
82
|
|
|
|
83
|
|
|
$headers = array_merge([''], $destinations); |
84
|
|
|
$data = []; |
85
|
|
|
foreach ($response->getRows() as $index => $row) { |
86
|
|
|
$r = [$origins[$index]]; |
87
|
|
|
foreach ($row->getElements() as $element) { |
88
|
|
|
if (Element::STATUS_OK === $element->getStatus()) { |
89
|
|
|
$r[] = $element->getDistance().', '.$element->getDuration(); |
90
|
|
|
} else { |
91
|
|
|
$r[] = 'unavailable'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
$data[] = $r; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$table = new Table($output); |
98
|
|
|
$table |
99
|
|
|
->setHeaders($headers) |
100
|
|
|
->setRows($data) |
101
|
|
|
; |
102
|
|
|
$table->render(); |
103
|
|
|
|
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|