Passed
Push — master ( d31a50...87284d )
by Damien
02:11
created

RoutingQuery   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 186
rs 10
c 1
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setTransportMode() 0 5 1
A setTrafficMode() 0 5 1
A getRoutingMode() 0 3 1
B buildRequest() 0 39 6
A setAvoid() 0 5 1
A buildResponse() 0 3 1
A getTrafficMode() 0 3 1
A getAvoid() 0 3 1
A getTransportMode() 0 3 1
A setRoutingMode() 0 5 1
1
<?php
2
3
namespace DH\NavigationBundle\Provider\Here\Routing;
4
5
use DH\NavigationBundle\Contract\Routing\RoutingQueryInterface;
6
use DH\NavigationBundle\Contract\Routing\RoutingResponseInterface;
7
use DH\NavigationBundle\Contract\Routing\AbstractRoutingQuery;
8
use Psr\Http\Message\ResponseInterface;
9
10
class RoutingQuery extends AbstractRoutingQuery
11
{
12
    /**
13
     * fastest | shortest | balanced.
14
     *
15
     * @var string
16
     */
17
    private $routingMode;
18
19
    /**
20
     * car | pedestrian | carHOV | publicTransport | publicTransportTimeTable | truck | bicycle.
21
     *
22
     * @var string
23
     */
24
    private $transportMode;
25
26
    /**
27
     * enabled | disabled.
28
     *
29
     * @var string
30
     */
31
    private $trafficMode;
32
33
    /**
34
     * @var string
35
     */
36
    private $avoid;
37
38
    /**
39
     * URL for API.
40
     */
41
    const ENDPOINT_URL = 'https://route.api.here.com/routing/7.2/calculateroute.json';
42
43
    /**
44
     * URL for API (CIT).
45
     */
46
    const CIT_ENDPOINT_URL = 'https://route.cit.api.here.com/routing/7.2/calculateroute.json';
47
48
    const TRANSPORT_MODE_CAR = 'car';
49
    const TRANSPORT_MODE_CAR_HOV = 'carHOV';
50
    const TRANSPORT_MODE_PEDESTRIAN = 'pedestrian';
51
    const TRANSPORT_MODE_BICYCLE = 'bicycle';
52
    const TRANSPORT_MODE_TRUCK = 'truck';
53
54
    const ROUTING_MODE_FASTEST = 'fastest';
55
    const ROUTING_MODE_SHORTEST = 'shortest';
56
    const ROUTING_MODE_BALANCED = 'balanced';
57
58
    const TRAFFIC_MODE_ENABLED = 'enabled';
59
    const TRAFFIC_MODE_DISABLED = 'disabled';
60
    const TRAFFIC_MODE_DEFAULT = 'default';
61
62
    /**
63
     * @param string $mode
64
     *
65
     * @return RoutingQueryInterface
66
     */
67
    public function setRoutingMode($mode = self::ROUTING_MODE_FASTEST): RoutingQueryInterface
68
    {
69
        $this->routingMode = $mode;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getRoutingMode(): string
78
    {
79
        return $this->routingMode ?? self::ROUTING_MODE_FASTEST;
80
    }
81
82
    /**
83
     * @param string $mode
84
     *
85
     * @return RoutingQueryInterface
86
     */
87
    public function setTransportMode($mode = self::TRANSPORT_MODE_CAR): RoutingQueryInterface
88
    {
89
        $this->transportMode = $mode;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getTransportMode(): string
98
    {
99
        return $this->transportMode ?? self::TRANSPORT_MODE_CAR;
100
    }
101
102
    /**
103
     * @param string $mode
104
     *
105
     * @return RoutingQueryInterface
106
     */
107
    public function setTrafficMode($mode = self::TRAFFIC_MODE_DEFAULT): RoutingQueryInterface
108
    {
109
        $this->trafficMode = $mode;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getTrafficMode(): string
118
    {
119
        return $this->trafficMode ?? self::TRAFFIC_MODE_DEFAULT;
120
    }
121
122
    /**
123
     * @param string $avoid (for more values use | as separator)
124
     *
125
     * @return RoutingQueryInterface
126
     */
127
    public function setAvoid(string $avoid): RoutingQueryInterface
128
    {
129
        $this->avoid = $avoid;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getAvoid(): string
138
    {
139
        return $this->avoid;
140
    }
141
142
    /**
143
     * @see https://developer.here.com/documentation/routing/topics/resource-calculate-route.html
144
     *
145
     * @return string
146
     */
147
    protected function buildRequest(): string
148
    {
149
        $data = array_merge(
150
            $this->getProvider()->getCredentials(),
151
            [
152
                'avoid' => $this->avoid,
153
                'mode' => $this->getRoutingMode().';'.$this->getTransportMode().';traffic:'.$this->getTrafficMode(),
154
            ]
155
        );
156
157
        if (null !== $this->getArrivalTime()) {
158
            $data['arrival'] = $this->getArrivalTime()
159
                ->format('Y-m-d\TH:i:s')
160
            ;
161
        } elseif (null !== $this->getDepartureTime()) {
162
            $data['departure'] = $this->getDepartureTime()
163
                ->format('Y-m-d\TH:i:s')
164
            ;
165
        } else {
166
            $data['departure'] = 'now';
167
        }
168
169
        $count = \count($this->waypoints);
170
        for ($i = 0; $i < $count; ++$i) {
171
            $data['waypoint'.$i] = str_replace(' ', '', $this->waypoints[$i]);
172
        }
173
174
        $data = array_filter($data, static function ($value) {
175
            return null !== $value;
176
        });
177
178
        $parameters = [];
179
        foreach ($data as $key => $value) {
180
            $parameters[] = $key.'='.$value;
181
        }
182
        $parameters = implode('&', $parameters);
183
        $url = ($this->getProvider()->isCitEnabled() ? self::CIT_ENDPOINT_URL : self::ENDPOINT_URL).'?'.$parameters;
0 ignored issues
show
Bug introduced by
The method isCitEnabled() does not exist on DH\NavigationBundle\Provider\ProviderInterface. It seems like you code against a sub-type of DH\NavigationBundle\Provider\ProviderInterface such as DH\NavigationBundle\Provider\Here\Here. ( Ignorable by Annotation )

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

183
        $url = ($this->getProvider()->/** @scrutinizer ignore-call */ isCitEnabled() ? self::CIT_ENDPOINT_URL : self::ENDPOINT_URL).'?'.$parameters;
Loading history...
184
185
        return $url;
186
    }
187
188
    /**
189
     * @param ResponseInterface $response
190
     *
191
     * @return RoutingResponseInterface
192
     */
193
    protected function buildResponse(ResponseInterface $response): RoutingResponseInterface
194
    {
195
        return new RoutingResponse($response);
196
    }
197
}
198