Passed
Push — master ( 6d8670...fc56ea )
by Damien
02:05
created

DistanceMatrixQuery::buildResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Provider\Here\DistanceMatrix;
4
5
use DH\NavigationBundle\Contract\DistanceMatrix\AbstractDistanceMatrixQuery;
6
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixQueryInterface;
7
use DH\NavigationBundle\Contract\DistanceMatrix\DistanceMatrixResponseInterface;
8
use DH\NavigationBundle\Exception\DestinationException;
9
use DH\NavigationBundle\Exception\OriginException;
10
use Psr\Http\Message\ResponseInterface;
11
12
class DistanceMatrixQuery extends AbstractDistanceMatrixQuery
13
{
14
    /**
15
     * fastest | shortest | balanced.
16
     *
17
     * @var string
18
     */
19
    private $routingMode;
20
21
    /**
22
     * car | pedestrian | carHOV | publicTransport | publicTransportTimeTable | truck | bicycle.
23
     *
24
     * @var string
25
     */
26
    private $transportMode;
27
28
    /**
29
     * enabled | disabled.
30
     *
31
     * @var string
32
     */
33
    private $trafficMode;
34
35
    /**
36
     * @var string
37
     */
38
    private $avoid;
39
40
    /**
41
     * @var \DateTime
42
     */
43
    private $departure_time;
44
45
    /**
46
     * URL for API.
47
     */
48
    const ENDPOINT_URL = 'https://matrix.route.api.here.com/routing/7.2/calculatematrix.json';
49
50
    /**
51
     * URL for API (CIT).
52
     */
53
    const CIT_ENDPOINT_URL = 'https://matrix.route.cit.api.here.com/routing/7.2/calculatematrix.json';
54
55
    const TRANSPORT_MODE_CAR = 'car';
56
    const TRANSPORT_MODE_CAR_HOV = 'carHOV';
57
    const TRANSPORT_MODE_PEDESTRIAN = 'pedestrian';
58
    const TRANSPORT_MODE_BICYCLE = 'bicycle';
59
    const TRANSPORT_MODE_TRUCK = 'truck';
60
61
    const ROUTING_MODE_FASTEST = 'fastest';
62
    const ROUTING_MODE_SHORTEST = 'shortest';
63
    const ROUTING_MODE_BALANCED = 'balanced';
64
65
    const TRAFFIC_MODE_ENABLED = 'enabled';
66
    const TRAFFIC_MODE_DISABLED = 'disabled';
67
68
    /**
69
     * @param string $mode
70
     *
71
     * @return DistanceMatrixQueryInterface
72
     */
73
    public function setRoutingMode($mode = self::ROUTING_MODE_FASTEST): DistanceMatrixQueryInterface
74
    {
75
        $this->routingMode = $mode;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getRoutingMode(): string
84
    {
85
        return $this->routingMode ?? self::ROUTING_MODE_FASTEST;
86
    }
87
88
    /**
89
     * @param string $mode
90
     *
91
     * @return DistanceMatrixQueryInterface
92
     */
93
    public function setTransportMode($mode = self::TRANSPORT_MODE_CAR): DistanceMatrixQueryInterface
94
    {
95
        $this->transportMode = $mode;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getTransportMode(): string
104
    {
105
        return $this->transportMode ?? self::TRANSPORT_MODE_CAR;
106
    }
107
108
    /**
109
     * @param string $mode
110
     *
111
     * @return DistanceMatrixQueryInterface
112
     */
113
    public function setTrafficMode($mode = self::TRAFFIC_MODE_ENABLED): DistanceMatrixQueryInterface
114
    {
115
        $this->trafficMode = $mode;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getTrafficMode(): string
124
    {
125
        return $this->trafficMode ?? self::TRAFFIC_MODE_ENABLED;
126
    }
127
128
    /**
129
     * @param string $avoid (for more values use | as separator)
130
     *
131
     * @return DistanceMatrixQueryInterface
132
     */
133
    public function setAvoid(string $avoid): DistanceMatrixQueryInterface
134
    {
135
        $this->avoid = $avoid;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getAvoid(): string
144
    {
145
        return $this->avoid;
146
    }
147
148
    /**
149
     * @see https://developer.here.com/documentation/routing/topics/resource-calculate-matrix.html
150
     *
151
     * @throws ResponseException
152
     * @throws \GuzzleHttp\Exception\GuzzleException
153
     * @throws DestinationException
154
     * @throws OriginException
155
     *
156
     * @return ResponseInterface
157
     */
158
    protected function buildRequest(): string
159
    {
160
        $data = array_merge(
161
            $this->getProvider()->getCredentials(),
162
            [
163
                'avoid' => $this->avoid,
164
                'mode' => $this->getRoutingMode().';'.$this->getTransportMode().';traffic:'.$this->getTrafficMode(),
165
                'summaryAttributes' => 'traveltime,distance,costfactor',
166
            ]
167
        );
168
169
        if (null !== $this->departure_time) {
170
            $data['departure'] = $this->departure_time
171
                ->format('Y-m-d\TH:i:s')
172
            ;
173
        } else {
174
            $data['departure'] = 'now';
175
        }
176
177
        $count = \count($this->origins);
178
        for ($i = 0; $i < $count; ++$i) {
179
            $data['start'.$i] = str_replace(' ', '', $this->origins[$i]);
180
        }
181
182
        $count = \count($this->destinations);
183
        for ($i = 0; $i < $count; ++$i) {
184
            $data['destination'.$i] = str_replace(' ', '', $this->destinations[$i]);
185
        }
186
187
        $data = array_filter($data, static function ($value) {
188
            return null !== $value;
189
        });
190
191
        $parameters = [];
192
        foreach ($data as $key => $value) {
193
            $parameters[] = $key.'='.$value;
194
        }
195
        $parameters = implode('&', $parameters);
196
        $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

196
        $url = ($this->getProvider()->/** @scrutinizer ignore-call */ isCitEnabled() ? self::CIT_ENDPOINT_URL : self::ENDPOINT_URL).'?'.$parameters;
Loading history...
197
198
        return $url;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $url returns the type string which is incompatible with the documented return type Psr\Http\Message\ResponseInterface.
Loading history...
199
    }
200
201
    /**
202
     * @param ResponseInterface $response
203
     *
204
     * @return DistanceMatrixResponseInterface
205
     */
206
    protected function buildResponse(ResponseInterface $response): DistanceMatrixResponseInterface
207
    {
208
        return new DistanceMatrixResponse($response, $this->getOrigins(), $this->getDestinations());
209
    }
210
}
211