|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aghasoroush\InjamLaravelPackage; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Aghasoroush\InjamLaravelPackage\Exceptions\InvalidArgumentException as InvalidArgument; |
|
7
|
|
|
use Aghasoroush\InjamLaravelPackage\Exceptions\InvalidResponseException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class InjamLaravelPackage |
|
11
|
|
|
* @package Aghasoroush\InjamLaravelPackage |
|
12
|
|
|
* @author [email protected] |
|
13
|
|
|
*/ |
|
14
|
|
|
class InjamLaravelPackage{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Injam.io API key |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $apiKey; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Injam.io API interface |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $apiAddress; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Number of seconds that http client waits for response |
|
30
|
|
|
* @var float |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $httpTimeout; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* GuzzleHttp Client instance |
|
36
|
|
|
* @var \GuzzleHttp\Client |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $httpClient; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set API key |
|
42
|
|
|
* @param string $apiKey |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setApiKey($apiKey) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->apiKey = $apiKey; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get API key |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getApiKey() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->apiKey; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set API interface |
|
62
|
|
|
* @param string $address |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setApiAddress($address) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->apiAddress = $address; |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get API interface |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getApiAddress() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->apiAddress; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set http request timeout in seconds |
|
82
|
|
|
* @param float $timeout |
|
83
|
|
|
* @return $this |
|
84
|
|
|
* @throws InvalidArgument |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setHttpTimeout($timeout) |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_numeric($timeout) && $timeout > 0) { |
|
89
|
|
|
$this->httpTimeout = $timeout; |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new InvalidArgument('$timeout must be numeric and greater than 0'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get http request timeout |
|
99
|
|
|
* @return float |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getHttpTimeout() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->httpTimeout; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function __construct() |
|
107
|
|
|
{ |
|
108
|
|
|
$apiKey = config('injam-laravel-package.api_key'); |
|
109
|
|
|
$apiAddress = config('injam-laravel-package.api_address'); |
|
110
|
|
|
$httpTimeout = config('injam-laravel-package.http_request_timeout'); |
|
111
|
|
|
|
|
112
|
|
|
$this->apiKey = $apiKey; |
|
113
|
|
|
$this->apiAddress = $apiAddress; |
|
114
|
|
|
$this->httpTimeout = $httpTimeout; |
|
115
|
|
|
$client = new Client([ |
|
116
|
|
|
'timeout' => $httpTimeout, |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
$this->httpClient = $client; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Create http request and send it to Injam.io servers |
|
124
|
|
|
* @param string $path |
|
125
|
|
|
* @param string $method |
|
126
|
|
|
* @param mixed $body |
|
127
|
|
|
* @return array |
|
128
|
|
|
* @throws InvalidArgumentException |
|
129
|
|
|
* @throws InvalidResponseException |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function sendRequest($path, $method, $body=null) |
|
132
|
|
|
{ |
|
133
|
|
|
$method = strtoupper($method); |
|
134
|
|
|
$resp = null; |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
if (in_array($method, ['PUT', 'POST', 'UPDATE', 'PATCH'])) { |
|
137
|
|
|
if (!is_array($body)) { |
|
138
|
|
|
throw new InvalidArgument('$body should be array'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$options = []; |
|
143
|
|
|
|
|
144
|
|
|
if (!in_array($method, ['GET', 'DELETE'])) { |
|
145
|
|
|
$options['json'] = $body; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$options['headers'] = [ |
|
149
|
|
|
'X-AUTHORIZATION' => $this->apiKey |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
try { |
|
153
|
|
|
$resp = $this->httpClient->request($method, $this->apiAddress . $path, $options); |
|
154
|
|
|
// dd($resp->getBody()->getContents()); |
|
155
|
|
|
return json_decode($resp->getBody()->getContents(), true); |
|
156
|
|
|
} catch (\Exception $e) { |
|
157
|
|
|
throw new InvalidResponseException('Invalid response. ' . $e->getMessage()); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Add a tracker |
|
163
|
|
|
* @param string $trackingPhysicalId |
|
164
|
|
|
* @param string $trackerMobile |
|
165
|
|
|
* @param string $title |
|
166
|
|
|
* @param string $description |
|
167
|
|
|
* @param string $trackingName |
|
168
|
|
|
* @param string $trackingMobile |
|
169
|
|
|
* @param string $trackingAvatar |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
|
|
public function addTracker($trackingPhysicalId, |
|
173
|
|
|
$trackerMobile, |
|
174
|
|
|
$title=null, |
|
175
|
|
|
$description=null, |
|
176
|
|
|
$trackingName=null, |
|
177
|
|
|
$trackingMobile=null, |
|
178
|
|
|
$trackingAvatar=null) |
|
179
|
|
|
{ |
|
180
|
|
|
$params = [ |
|
181
|
|
|
'tracking_physical_id' => $trackingPhysicalId, |
|
182
|
|
|
'tracker_mobile' => $trackerMobile, |
|
183
|
|
|
'title' => $title, |
|
184
|
|
|
'description' => $description, |
|
185
|
|
|
'tracking_name' => $trackingName, |
|
186
|
|
|
'tracking_mobile' => $trackingMobile, |
|
187
|
|
|
'tracking_avatar' => $trackingAvatar |
|
188
|
|
|
]; |
|
189
|
|
|
|
|
190
|
|
|
return $this->sendRequest('trackers/add', 'POST', $params); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set status of a tracker to done(the tracker link will not be working after this api call) |
|
195
|
|
|
* @param string $trackingPhysicalId |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
|
|
public function doneTracker($trackingPhysicalId) |
|
199
|
|
|
{ |
|
200
|
|
|
$params = []; |
|
201
|
|
|
$path = 'trackers/' . $trackingPhysicalId . '/done'; |
|
202
|
|
|
return $this->sendRequest($path, 'POST', $params); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Add Geofence webhook for a tracker |
|
207
|
|
|
* @param string $objectType |
|
208
|
|
|
* @param string $objectPhysicalId |
|
209
|
|
|
* @param string $targetPoint |
|
210
|
|
|
* @param integer $radius |
|
211
|
|
|
* @param string $endpoint |
|
212
|
|
|
* @param string $detect |
|
213
|
|
|
* @return array |
|
214
|
|
|
* @throws InvalidArgument |
|
215
|
|
|
*/ |
|
216
|
|
|
public function addGeoFenceWebhook($objectType, |
|
217
|
|
|
$objectPhysicalId, |
|
218
|
|
|
$targetPoint, |
|
219
|
|
|
$radius, |
|
220
|
|
|
$endpoint, |
|
221
|
|
|
$detect=null) |
|
222
|
|
|
{ |
|
223
|
|
|
$params = [ |
|
224
|
|
|
'object_type' => $objectType, |
|
225
|
|
|
'object_physical_id' => $objectPhysicalId, |
|
226
|
|
|
'target_point' => $targetPoint, |
|
227
|
|
|
'radius' => $radius, |
|
228
|
|
|
'endpoint' => $endpoint, |
|
229
|
|
|
'detect' => $detect |
|
230
|
|
|
]; |
|
231
|
|
|
|
|
232
|
|
|
return $this->sendRequest('webhooks/fence/add', 'POST', $params); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function deleteWebhook($webhookId) |
|
236
|
|
|
{ |
|
237
|
|
|
$params = []; |
|
238
|
|
|
$path = 'webhooks/fence/' . $webhookId; |
|
239
|
|
|
return $this->sendRequest($path, 'DELETE', $params); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|