1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Afonso\Emt; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use GuzzleHttp\Client as Guzzle; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The SDK client for the EMT OpenData API. |
11
|
|
|
* |
12
|
|
|
* @author Carlos Afonso Pérez <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Client |
15
|
|
|
{ |
16
|
|
|
const ENDPOINT = 'https://openbus.emtmadrid.es:9443'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The request launcher instance. |
20
|
|
|
* |
21
|
|
|
* @var \Afonso\Emt\RequestLauncher |
22
|
|
|
*/ |
23
|
|
|
protected $launcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new Client instance with the given client ID and passkey. |
27
|
|
|
* |
28
|
|
|
* @param string $clientId |
29
|
|
|
* @param string $passkey |
30
|
|
|
*/ |
31
|
21 |
|
public function __construct($clientId, $passkey) |
32
|
|
|
{ |
33
|
21 |
|
$this->launcher = new RequestLauncher($clientId, $passkey); |
34
|
21 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return the itinerary of one or more lines. |
38
|
|
|
* |
39
|
|
|
* @var int[] $lines |
40
|
|
|
* @var \DateTime $date |
41
|
|
|
* @return \stdClass |
42
|
|
|
* @throws \RuntimeException |
43
|
|
|
*/ |
44
|
3 |
|
public function getRouteLines(array $lines, DateTime $date) |
45
|
|
|
{ |
46
|
|
|
$params = [ |
47
|
3 |
|
'Lines' => join('|', $lines), |
48
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
49
|
1 |
|
]; |
50
|
3 |
|
return $this->callBusService('GetRouteLines.php', $params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return calendar details for the given date interval. |
55
|
|
|
* |
56
|
|
|
* @var \DateTime $startDate |
57
|
|
|
* @var \DateTime $endDate |
58
|
|
|
* @return \stdClass |
59
|
|
|
* @throws \RuntimeException |
60
|
|
|
*/ |
61
|
3 |
|
public function getCalendar(DateTime $startDate, DateTime $endDate) |
62
|
|
|
{ |
63
|
|
|
$params = [ |
64
|
3 |
|
'SelectDateBegin' => $startDate->format('d/m/Y'), |
65
|
3 |
|
'SelectDateEnd' => $endDate->format('d/m/Y'), |
66
|
1 |
|
]; |
67
|
3 |
|
return $this->callBusService('GetCalendar.php', $params); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return a list with line details. |
72
|
|
|
* |
73
|
|
|
* @var int[] $lines |
74
|
|
|
* @var \DateTime $date |
75
|
|
|
* @return \stdClass |
76
|
|
|
* @throws \RuntimeException |
77
|
|
|
*/ |
78
|
3 |
View Code Duplication |
public function getListLines(array $lines, DateTime $date) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$params = [ |
81
|
3 |
|
'Lines' => join('|', $lines), |
82
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
83
|
1 |
|
]; |
84
|
3 |
|
return $this->callBusService('GetListLines.php', $params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return details about all line groups. |
89
|
|
|
* |
90
|
|
|
* @return \stdClass |
91
|
|
|
* @throws \RuntimeException |
92
|
|
|
*/ |
93
|
3 |
|
public function getGroups() |
94
|
|
|
{ |
95
|
3 |
|
return $this->callBusService('GetGroups.php'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return start and end operation times of one or more lines. |
100
|
|
|
* |
101
|
|
|
* @var int[] $lines |
102
|
|
|
* @var \DateTime $date |
103
|
|
|
* @return \stdClass |
104
|
|
|
* @throws \RuntimeException |
105
|
|
|
*/ |
106
|
3 |
View Code Duplication |
public function getTimesLines(array $lines, DateTime $date) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$params = [ |
109
|
3 |
|
'Lines' => join('|', $lines), |
110
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
111
|
1 |
|
]; |
112
|
3 |
|
return $this->callBusService('GetTimesLines.php', $params); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return timetable details of one or more lines. |
117
|
|
|
* |
118
|
|
|
* @var int[] $lines |
119
|
|
|
* @var \DateTime $date |
120
|
|
|
* @return \stdClass |
121
|
|
|
* @throws \RuntimeException |
122
|
|
|
*/ |
123
|
3 |
View Code Duplication |
public function getTimeTableLines(array $lines, DateTime $date) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$params = [ |
126
|
3 |
|
'Lines' => join('|', $lines), |
127
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
128
|
1 |
|
]; |
129
|
3 |
|
return $this->callBusService('GetTimeTableLines.php', $params); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return details of one or more bus stops including name, served lines |
134
|
|
|
* and geographic coordinates. |
135
|
|
|
* |
136
|
|
|
* @var int[] $stopIds |
137
|
|
|
* @return \stdClass |
138
|
|
|
* @throws \RuntimeException |
139
|
|
|
*/ |
140
|
3 |
|
public function getNodesLines(array $stopIds) |
141
|
|
|
{ |
142
|
|
|
$params = [ |
143
|
3 |
|
'Nodes' => join('|', $stopIds), |
144
|
1 |
|
]; |
145
|
3 |
|
return $this->callBusService('GetNodesLines.php', $params); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the RequestLauncher instance to be used by this client. |
150
|
|
|
* |
151
|
|
|
* @param \Afonso\Emt\RequestLauncher $launcher |
152
|
|
|
*/ |
153
|
21 |
|
public function setRequestLauncher(RequestLauncher $launcher) |
154
|
|
|
{ |
155
|
21 |
|
$this->launcher = $launcher; |
156
|
21 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Make an arbitrary call to the Bus service. |
160
|
|
|
* |
161
|
|
|
* @param string $endpoint |
162
|
|
|
* @param array $params |
163
|
|
|
* @return \stdClass |
164
|
|
|
*/ |
165
|
21 |
|
protected function callBusService($endpoint, array $params = []) |
166
|
|
|
{ |
167
|
21 |
|
$url = self::ENDPOINT . '/emt-proxy-server/last/bus/' . $endpoint; |
168
|
21 |
|
return $this->launcher->launchRequest($url, $params)->resultValues; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.