Passed
Pull Request — master (#4)
by Maxime
03:42
created

RoutingCommand::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Command;
4
5
use DateTime;
6
use DH\NavigationBundle\Model\Routing\Summary;
7
use DH\NavigationBundle\NavigationManager;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Exception\InvalidArgumentException;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
class RoutingCommand extends Command
17
{
18
    protected static $defaultName = 'navigation:routing';
19
20
    /**
21
     * @var NavigationManager
22
     */
23
    private $manager;
24
25
    /**
26
     * @param NavigationManager $manager
27
     */
28
    public function __construct(NavigationManager $manager)
29
    {
30
        $this->manager = $manager;
31
32
        parent::__construct();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('navigation:routing')
42
            ->setDescription('Computes a route')
43
            ->addOption('provider', null, InputOption::VALUE_REQUIRED)
44
            ->addOption('waypoint', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Point de passage')
45
            ->addOption('departure', null, InputOption::VALUE_REQUIRED, 'Departure date and time (YYYY-MM-DDD HH:II:SS)')
46
            ->addOption('arrival', null, InputOption::VALUE_REQUIRED, 'Arrival date and time (YYYY-MM-DDD HH:II:SS)')
47
            ->addOption('traffic', null, InputOption::VALUE_REQUIRED, 'Traffic mode (enabled/disabled/default depending on provider)')
48
            ->addOption('language', null, InputOption::VALUE_REQUIRED, 'Language (fr-FR, en-US, etc.)')
49
            ->setHelp(<<<'EOF'
50
The <info>navigation:routing</info> command will compute a route from the given addresses.
51
52
You can choose a provider with the "provider" option.
53
54
<info>php bin/console navigation:routing --waypoint="45.834278,1.260816" --waypoint="44.830109,-0.603649" --provider=here</info>
55
EOF
56
            )
57
        ;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        if (\count($input->getOption('waypoint')) < 2) {
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('waypoint') can also be of type boolean and string; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        if (\count(/** @scrutinizer ignore-type */ $input->getOption('waypoint')) < 2) {
Loading history...
66
            throw new InvalidArgumentException('A route needs at least two waypoints (start and end locations).');
67
        }
68
69
        if ($input->getOption('provider')) {
70
            $this->manager->using($input->getOption('provider'));
71
        }
72
73
        $query = $this->manager->createRoutingQuery();
74
75
        foreach ($input->getOption('waypoint') as $waypoint) {
76
            $query->addWaypoint($waypoint);
77
        }
78
79
        if ($input->getOption('departure')) {
80
            $query->setDepartureTime(new DateTime($input->getOption('departure')));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('departure') can also be of type string[]; however, parameter $time of DateTime::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $query->setDepartureTime(new DateTime(/** @scrutinizer ignore-type */ $input->getOption('departure')));
Loading history...
81
        }
82
83
        if ($input->getOption('arrival')) {
84
            $query->setArrivalTime(new DateTime($input->getOption('arrival')));
85
        }
86
87
        if ($input->getOption('traffic')) {
88
            $query->setTrafficMode($input->getOption('traffic'));
0 ignored issues
show
Bug introduced by
The method setTrafficMode() does not exist on DH\NavigationBundle\Cont...g\RoutingQueryInterface. It seems like you code against a sub-type of DH\NavigationBundle\Cont...g\RoutingQueryInterface such as DH\NavigationBundle\Prov...re\Routing\RoutingQuery. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            $query->/** @scrutinizer ignore-call */ 
89
                    setTrafficMode($input->getOption('traffic'));
Loading history...
89
        }
90
91
        if ($input->getOption('language')) {
92
            $query->setLanguage($input->getOption('language'));
93
        }
94
95
        $response = $query->execute();
96
97
        $io = new SymfonyStyle($input, $output);
98
99
        $routes = $response->getRoutes();
100
101
        $io->section('Summary');
102
103
        $data = [];
104
        foreach ($routes as $index => $route) {
105
            /**
106
             * @var Summary
107
             */
108
            $summary = $route->getSummary();
109
            $data[] = [
110
                $index + 1,
111
                $summary->getDistance()->getFormattedValue(2),
112
                $summary->getTravelTime()->getFormattedValue(2),
113
            ];
114
        }
115
116
        $table = new Table($output);
117
        $table
118
            ->setHeaders(['route', 'distance', 'duration'])
119
            ->setRows($data)
120
        ;
121
        $table->render();
122
123
        $io->newLine();
124
125
        foreach ($routes as $routeIndex => $route) {
126
            $io->section('Route #'.($routeIndex + 1));
127
128
            $legs = $route->getLegs();
129
            foreach ($legs as $legIndex => $leg) {
130
                $steps = $leg->getSteps();
131
                $io->writeln(sprintf('<comment>Leg #%d</comment> - %d steps.', $legIndex + 1, \count($steps)));
132
133
                $data = [];
134
                foreach ($steps as $stepIndex => $step) {
135
                    $data[] = [
136
                        $stepIndex + 1,
137
                        implode(', ', $step->getPosition()),
138
                        $step->getDistance()->getFormattedValue(2),
139
                        $step->getDuration()->getFormattedValue(2),
140
                        $step->getInstruction(),
141
                    ];
142
                }
143
144
                $table = new Table($output);
145
                $table
146
                    ->setHeaders(['step', 'position', 'distance', 'duration', 'instruction'])
147
                    ->setRows($data)
148
                    ->setColumnMaxWidth(4, 100)
149
                ;
150
                $table->render();
151
                $io->newLine();
152
            }
153
        }
154
155
        return 0;
156
    }
157
}
158