-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
170 lines (146 loc) · 3.83 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Kameli\NummerpladeApi;
use Exception;
use Kameli\NummerpladeApi\Exceptions\NotFoundException;
class Client
{
const API_URL = 'http://api.nrpla.de/';
/** @var string */
protected $apiToken;
/** @var int */
protected $timeout = 8;
/**
* @param string $apiToken
*/
public function __construct($apiToken)
{
$this->apiToken = $apiToken;
}
/**
* @param int $timeout
* @return Client
*/
public function setTimeout($timeout)
{
$this->timeout = (int) $timeout;
return $this;
}
/**
* @param int $timeout
* @return Client
*/
public function withTimeout($timeout)
{
$clone = clone $this;
$clone->setTimeout($timeout);
return $clone;
}
/**
* @param string $registration
* @param bool $advanced
* @return object
* @throws \Exception
*/
public function vehicleByRegistration($registration, $advanced = false)
{
return $this->request($registration, ['advanced' => (bool) $advanced]);
}
/**
* @param string $vin
* @param bool $advanced
* @return object
* @throws \Exception
*/
public function vehicleByVin($vin, $advanced = false)
{
return $this->request('vin/' . $vin, ['advanced' => (bool) $advanced]);
}
/**
* @param string $vehicle_id
* @return object
* @throws \Exception
*/
public function dmr($vehicle_id)
{
return $this->request('dmr/' . $vehicle_id);
}
/**
* @param string $registration
* @return object
* @throws \Exception
*/
public function dmrByRegistration($registration)
{
return $this->request('dmr/registration/' . $registration);
}
/**
* @param string $vin
* @return object
* @throws \Exception
*/
public function dmrByVin($vin)
{
return $this->request('dmr/vin/' . $vin);
}
/**
* @param string $vehicle_id
* @return object
* @throws \Exception
*/
public function debt($vehicle_id)
{
return $this->request('debt/' . $vehicle_id);
}
/**
* @param string $vehicle_id
* @return object
* @throws \Exception
*/
public function inspections($vehicle_id)
{
return $this->request('inspections/' . $vehicle_id);
}
/**
* @param string $input
* @return object
* @throws \Exception
*/
public function emissions($input)
{
return $this->request('emissions/' . $input);
}
/**
* @param string $endpoint
* @param array $parameters
* @return object
* @throws \Exception
*/
protected function request($endpoint, $parameters = [])
{
$ch = curl_init();
$query = http_build_query(array_merge($parameters, ['api_token' => $this->apiToken]));
curl_setopt_array($ch, [
CURLOPT_URL => static::API_URL . $endpoint . '?' . $query,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
]);
if (! $result = curl_exec($ch)) {
throw new Exception(curl_error($ch));
}
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
if ($response_code === 401) {
throw new Exception('Nummerplade API - Wrong API Token');
}
$response = json_decode(substr($result, $header_size));
if ($response_code === 200) {
return $response->data;
} elseif ($response_code === 404) {
throw new NotFoundException($response->message, $response->status_code);
} else {
throw new Exception($response->message, $response->status_code);
}
}
}