DistanceMatrixQuery::buildResponse()   A
last analyzed

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 Psr\Http\Message\ResponseInterface;
9
10
class DistanceMatrixQuery extends AbstractDistanceMatrixQuery
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://matrix.route.api.here.com/routing/7.2/calculatematrix.json';
42
43
    /**
44
     * URL for API (CIT).
45
     */
46
    public const CIT_ENDPOINT_URL = 'https://matrix.route.cit.api.here.com/routing/7.2/calculatematrix.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): DistanceMatrixQueryInterface
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): DistanceMatrixQueryInterface
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): DistanceMatrixQueryInterface
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): DistanceMatrixQueryInterface
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-matrix.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
                'summaryAttributes' => 'traveltime,distance,costfactor',
136
            ]
137
        );
138
139
        if (null !== $this->getDepartureTime()) {
140
            $data['departure'] = $this->getDepartureTime()
141
                ->format('Y-m-d\TH:i:s')
142
            ;
143
        } else {
144
            $data['departure'] = 'now';
145
        }
146
147
        $count = \count($this->origins);
148
        for ($i = 0; $i < $count; ++$i) {
149
            $data['start'.$i] = str_replace(' ', '', $this->origins[$i]);
150
        }
151
152
        $count = \count($this->destinations);
153
        for ($i = 0; $i < $count; ++$i) {
154
            $data['destination'.$i] = str_replace(' ', '', $this->destinations[$i]);
155
        }
156
157
        $data = array_filter($data, static function ($value) {
158
            return null !== $value;
159
        });
160
161
        $parameters = [];
162
        foreach ($data as $key => $value) {
163
            $parameters[] = $key.'='.$value;
164
        }
165
        $parameters = implode('&', $parameters);
166
167
        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

167
        return ($this->getProvider()->/** @scrutinizer ignore-call */ isCitEnabled() ? self::CIT_ENDPOINT_URL : self::ENDPOINT_URL).'?'.$parameters;
Loading history...
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    protected function buildResponse(ResponseInterface $response): DistanceMatrixResponseInterface
174
    {
175
        return new DistanceMatrixResponse($response, $this->getOrigins(), $this->getDestinations());
176
    }
177
}
178