RoutingQuery::setRoutingMode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace DH\NavigationBundle\Provider\Here\Routing;
4
5
use DH\NavigationBundle\Contract\Routing\AbstractRoutingQuery;
6
use DH\NavigationBundle\Contract\Routing\RoutingQueryInterface;
7
use DH\NavigationBundle\Contract\Routing\RoutingResponseInterface;
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
    public const ENDPOINT_URL = 'https://route.api.here.com/routing/7.2/calculateroute.json';
42
43
    /**
44
     * URL for API (CIT).
45
     */
46
    public const CIT_ENDPOINT_URL = 'https://route.cit.api.here.com/routing/7.2/calculateroute.json';
47
48
    public const TRANSPORT_MODE_CAR = 'car';
49
    public const TRANSPORT_MODE_CAR_HOV = 'carHOV';
50
    public const TRANSPORT_MODE_PEDESTRIAN = 'pedestrian';
51
    public const TRANSPORT_MODE_BICYCLE = 'bicycle';
52
    public const TRANSPORT_MODE_TRUCK = 'truck';
53
54
    public const ROUTING_MODE_FASTEST = 'fastest';
55
    public const ROUTING_MODE_SHORTEST = 'shortest';
56
    public const ROUTING_MODE_BALANCED = 'balanced';
57
58
    public const TRAFFIC_MODE_ENABLED = 'enabled';
59
    public const TRAFFIC_MODE_DISABLED = 'disabled';
60
    public const TRAFFIC_MODE_DEFAULT = 'default';
61
62
    /**
63
     * @param string $mode
64
     */
65
    public function setRoutingMode($mode = self::ROUTING_MODE_FASTEST): RoutingQueryInterface
66
    {
67
        $this->routingMode = $mode;
68
69
        return $this;
70
    }
71
72
    public function getRoutingMode(): string
73
    {
74
        return $this->routingMode ?? self::ROUTING_MODE_FASTEST;
75
    }
76
77
    /**
78
     * @param string $mode
79
     */
80
    public function setTransportMode($mode = self::TRANSPORT_MODE_CAR): RoutingQueryInterface
81
    {
82
        $this->transportMode = $mode;
83
84
        return $this;
85
    }
86
87
    public function getTransportMode(): string
88
    {
89
        return $this->transportMode ?? self::TRANSPORT_MODE_CAR;
90
    }
91
92
    /**
93
     * @param string $mode
94
     */
95
    public function setTrafficMode($mode = self::TRAFFIC_MODE_DEFAULT): RoutingQueryInterface
96
    {
97
        $this->trafficMode = $mode;
98
99
        return $this;
100
    }
101
102
    public function getTrafficMode(): string
103
    {
104
        return $this->trafficMode ?? self::TRAFFIC_MODE_DEFAULT;
105
    }
106
107
    /**
108
     * @param string $avoid (for more values use | as separator)
109
     */
110
    public function setAvoid(string $avoid): RoutingQueryInterface
111
    {
112
        $this->avoid = $avoid;
113
114
        return $this;
115
    }
116
117
    public function getAvoid(): string
118
    {
119
        return $this->avoid;
120
    }
121
122
    /**
123
     * @see https://developer.here.com/documentation/routing/topics/resource-calculate-route.html
124
     *
125
     * {@inheritdoc}
126
     */
127
    protected function buildRequest(): string
128
    {
129
        $data = array_merge(
130
            $this->getProvider()->getCredentials(),
131
            [
132
                'language' => $this->getLanguage(),
133
                'avoid' => $this->avoid,
134
                'mode' => $this->getRoutingMode().';'.$this->getTransportMode().';traffic:'.$this->getTrafficMode(),
135
            ]
136
        );
137
138
        if (null !== $this->getArrivalTime()) {
139
            $data['arrival'] = $this->getArrivalTime()
140
                ->format('Y-m-d\TH:i:s')
141
            ;
142
        } elseif (null !== $this->getDepartureTime()) {
143
            $data['departure'] = $this->getDepartureTime()
144
                ->format('Y-m-d\TH:i:s')
145
            ;
146
        } else {
147
            $data['departure'] = 'now';
148
        }
149
150
        foreach ($this->waypoints as $i => $value) {
151
            $data['waypoint'.$i] = str_replace(' ', '', $value);
152
        }
153
154
        $data = array_filter($data, static function ($value) {
155
            return null !== $value;
156
        });
157
158
        $parameters = [];
159
        foreach ($data as $key => $value) {
160
            $parameters[] = $key.'='.$value;
161
        }
162
        $parameters = implode('&', $parameters);
163
164
        return ($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

164
        return ($this->getProvider()->/** @scrutinizer ignore-call */ isCitEnabled() ? self::CIT_ENDPOINT_URL : self::ENDPOINT_URL).'?'.$parameters;
Loading history...
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    protected function buildResponse(ResponseInterface $response): RoutingResponseInterface
171
    {
172
        return new RoutingResponse($response);
173
    }
174
}
175