1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* WannaSpeak API Bundle |
5
|
|
|
* |
6
|
|
|
* @author Jean-Baptiste Blanchon <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Yproximite\WannaSpeakBundle\Api; |
10
|
|
|
|
11
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
12
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
13
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
14
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Statistics |
20
|
|
|
* |
21
|
|
|
* @see http://fr.wannaspeak.com/ |
22
|
|
|
*/ |
23
|
|
|
class Statistics implements StatisticsInterface |
24
|
|
|
{ |
25
|
|
|
const API_BASE_STAT_PARAMETER = 'stat'; |
26
|
|
|
const API_BASE_CT_PARAMETER = 'ct'; |
27
|
|
|
const API_BASE_SOUND_PARAMETER = 'sound'; |
28
|
|
|
const BEGIN_DATE = '01-01-2015'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var WannaSpeakHttpClient |
32
|
|
|
*/ |
33
|
|
|
private $httpClient; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* __construct |
37
|
|
|
* |
38
|
|
|
* @param WannaSpeakHttpClient $httpClient |
39
|
|
|
*/ |
40
|
|
|
public function __construct(WannaSpeakHttpClient $httpClient) |
41
|
|
|
{ |
42
|
|
|
$this->httpClient = $httpClient; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $method |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
* |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function getNumbers($method) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$args = [ |
55
|
|
|
'api' => self::API_BASE_CT_PARAMETER, |
56
|
|
|
'method' => $method, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
60
|
|
|
$data = $this->processResponse($response); |
61
|
|
|
|
62
|
|
|
return $data['data']['dids']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Process the API response, provides error handling |
67
|
|
|
* |
68
|
|
|
* @param ResponseInterface $response |
69
|
|
|
* |
70
|
|
|
* @throws \Exception |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function processResponse(ResponseInterface $response) |
75
|
|
|
{ |
76
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
77
|
|
|
|
78
|
|
|
if ($data['error']) { |
79
|
|
|
throw new \Exception('WannaSpeak API: '.$data['error']['txt']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* We store the platformId in tag1 |
87
|
|
|
* and siteId in tag2 |
88
|
|
|
* |
89
|
|
|
* @param string $method |
90
|
|
|
* @param string $name |
91
|
|
|
* @param string $phoneDest |
92
|
|
|
* @param string $phoneDid |
93
|
|
|
* @param string $platformId |
94
|
|
|
* @param string $siteId |
95
|
|
|
* @param bool $callerId |
96
|
|
|
* @param string|null $leg1 |
97
|
|
|
* @param string|null $leg2 |
98
|
|
|
* @param string|null $phoneMobileNumberForMissedCall |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function callTracking($method, $name, $phoneDest, $phoneDid, $platformId, $siteId, $callerId = false, $leg1 = null, $leg2 = null, $phoneMobileNumberForMissedCall = null) |
103
|
|
|
{ |
104
|
|
|
$args = [ |
105
|
|
|
'api' => self::API_BASE_CT_PARAMETER, |
106
|
|
|
'method' => $method, |
107
|
|
|
'destination' => $phoneDest, |
108
|
|
|
'tag1' => $platformId, |
109
|
|
|
'tag2' => $siteId, |
110
|
|
|
'tag3' => ($callerId === true) ? 'callerid:'.$phoneDid : '', |
111
|
|
|
'did' => $phoneDid, |
112
|
|
|
'name' => $name, |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
if ($leg1 !== null) { |
116
|
|
|
$args['leg1'] = $leg1; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($leg2 !== null) { |
120
|
|
|
$args['leg2'] = $leg2; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($phoneMobileNumberForMissedCall !== null) { |
124
|
|
|
$args['sms'] = $phoneMobileNumberForMissedCall; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
128
|
|
|
$data = $this->processResponse($response); |
129
|
|
|
|
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
/** |
133
|
|
|
* @param string $didPhone |
134
|
|
|
* @param \DateTime $expirationDate |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function callTrackingExpiresAt($didPhone, \DateTime $expirationDate = null) |
139
|
|
|
{ |
140
|
|
|
if (!$expirationDate) { |
141
|
|
|
$expirationDate = new \DateTime('now'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$args = [ |
145
|
|
|
'api' => self::API_BASE_CT_PARAMETER, |
146
|
|
|
'method' => 'modify', |
147
|
|
|
'did' => $didPhone, |
148
|
|
|
'enddate' => $expirationDate->format('Y-m-d H:i:s'), |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
152
|
|
|
$data = $this->processResponse($response); |
153
|
|
|
|
154
|
|
|
return $data; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Will fetch all datas from your account |
159
|
|
|
* from $beginDate to $endDate |
160
|
|
|
* |
161
|
|
|
* if there are no dates, the API's default behaviour will return |
162
|
|
|
* today's calls. we provide defaults dates in order to have all |
163
|
|
|
* calls from the begining of the time to now |
164
|
|
|
* |
165
|
|
|
* @param \DateTime $beginDate |
166
|
|
|
* @param \DateTime $endDate |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
public function getAllStats(\DateTime $beginDate = null, \DateTime $endDate = null) |
171
|
|
|
{ |
172
|
|
|
if (!$beginDate) { |
173
|
|
|
$beginDate = new \DateTime(self::BEGIN_DATE); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (!$endDate) { |
177
|
|
|
$endDate = new \DateTime('NOW'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$args = [ |
181
|
|
|
'api' => self::API_BASE_STAT_PARAMETER, |
182
|
|
|
'method' => 'did', |
183
|
|
|
'starttime' => $beginDate->format('Y-m-d H:i:s'), |
184
|
|
|
'stoptime' => $endDate->format('Y-m-d H:i:s'), |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
188
|
|
|
$data = $this->processResponse($response); |
189
|
|
|
|
190
|
|
|
return $data; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Will fetch all datas from your account |
195
|
|
|
* from $beginDate to $endDate |
196
|
|
|
* |
197
|
|
|
* if there are no dates, the API's default behaviour will return |
198
|
|
|
* today's calls. we provide defaults dates in order to have all |
199
|
|
|
* calls from the begining of the time to now |
200
|
|
|
* |
201
|
|
|
* @param string $platformId |
202
|
|
|
* @param \DateTime $beginDate |
203
|
|
|
* @param \DateTime $endDate |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
public function getStatsByPlatform($platformId, \DateTime $beginDate = null, \DateTime $endDate = null) |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
if (!$beginDate) { |
210
|
|
|
$beginDate = new \DateTime(self::BEGIN_DATE); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (!$endDate) { |
214
|
|
|
$endDate = new \DateTime('NOW'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$args = [ |
218
|
|
|
'api' => self::API_BASE_STAT_PARAMETER, |
219
|
|
|
'method' => 'did', |
220
|
|
|
'nodid' => '1', |
221
|
|
|
'tag1' => $platformId, |
222
|
|
|
'starttime' => $beginDate->format('Y-m-d 00:00:00'), |
223
|
|
|
'stoptime' => $endDate->format('Y-m-d 23:59:59'), |
224
|
|
|
]; |
225
|
|
|
|
226
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
227
|
|
|
$data = $this->processResponse($response); |
228
|
|
|
|
229
|
|
|
return $data; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Will fetch all datas from your account |
234
|
|
|
* from $beginDate to $endDate |
235
|
|
|
* |
236
|
|
|
* if there are no dates, the API's default behaviour will return |
237
|
|
|
* today's calls. we provide defaults dates in order to have all |
238
|
|
|
* calls from the begining of the time to now |
239
|
|
|
* |
240
|
|
|
* @param string $siteId |
241
|
|
|
* @param \DateTime $beginDate |
242
|
|
|
* @param \DateTime $endDate |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
public function getStatsBySite($siteId, \DateTime $beginDate = null, \DateTime $endDate = null) |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
if (!$beginDate) { |
249
|
|
|
$beginDate = new \DateTime(self::BEGIN_DATE); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if (!$endDate) { |
253
|
|
|
$endDate = new \DateTime('NOW'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$args = [ |
257
|
|
|
'api' => self::API_BASE_STAT_PARAMETER, |
258
|
|
|
'method' => 'did', |
259
|
|
|
'nodid' => '1', |
260
|
|
|
'tag2' => $siteId, |
261
|
|
|
'starttime' => $beginDate->format('Y-m-d 00:00:00'), |
262
|
|
|
'stoptime' => $endDate->format('Y-m-d 23:59:59'), |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
266
|
|
|
$data = $this->processResponse($response); |
267
|
|
|
|
268
|
|
|
return $data; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* |
273
|
|
|
* @param string $didPhone |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
View Code Duplication |
public function callTrackingDelete($didPhone) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$args = [ |
280
|
|
|
'api' => self::API_BASE_CT_PARAMETER, |
281
|
|
|
'method' => 'delete', |
282
|
|
|
'did' => $didPhone, |
283
|
|
|
]; |
284
|
|
|
|
285
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
286
|
|
|
$data = $this->processResponse($response); |
287
|
|
|
|
288
|
|
|
return $data; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param int $link |
293
|
|
|
* |
294
|
|
|
* @return array |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function listSounds($link = 0) |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$args = [ |
299
|
|
|
'api' => self::API_BASE_SOUND_PARAMETER, |
300
|
|
|
'method' => 'available', |
301
|
|
|
'link' => $link, |
302
|
|
|
]; |
303
|
|
|
|
304
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
305
|
|
|
$data = $this->processResponse($response); |
306
|
|
|
|
307
|
|
|
return $data; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param UploadedFile $message |
312
|
|
|
* |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
public function uploadMessageToWannaspeak(UploadedFile $message) |
316
|
|
|
{ |
317
|
|
|
$name = str_replace('.mp3', '', $message->getClientOriginalName()); |
318
|
|
|
$args = [ |
319
|
|
|
'api' => 'sound', |
320
|
|
|
'method' => 'upload', |
321
|
|
|
'name' => $name, |
322
|
|
|
]; |
323
|
|
|
$options = [ |
324
|
|
|
'filename' => $name, |
325
|
|
|
'headers' => [ |
326
|
|
|
'Content-Type' => 'application/octet-stream', |
327
|
|
|
], |
328
|
|
|
]; |
329
|
|
|
|
330
|
|
|
$boundary = '--------------------------'.microtime(true); |
331
|
|
|
$streamFactory = StreamFactoryDiscovery::find(); |
332
|
|
|
$builder = new MultipartStreamBuilder($streamFactory); |
333
|
|
|
|
334
|
|
|
$builder->setBoundary($boundary); |
335
|
|
|
|
336
|
|
|
$fp = fopen($message->getRealPath(), 'rb'); |
337
|
|
|
$data = stream_get_contents($fp); |
338
|
|
|
fclose($fp); |
339
|
|
|
|
340
|
|
|
$builder->addResource('sound', $data, $options); |
341
|
|
|
|
342
|
|
|
$body = $builder->build(); |
343
|
|
|
$headers = ['Content-Type' => 'multipart/form-data; boundary="'.$boundary.'"']; |
344
|
|
|
|
345
|
|
|
$response = $this->httpClient->createAndSendRequest($args, $headers, $body); |
346
|
|
|
$data = $this->processResponse($response); |
347
|
|
|
|
348
|
|
|
return $data; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $name |
353
|
|
|
* |
354
|
|
|
* @return array |
355
|
|
|
*/ |
356
|
|
View Code Duplication |
public function deleteMessageWannaspeak($name) |
|
|
|
|
357
|
|
|
{ |
358
|
|
|
$args = [ |
359
|
|
|
'api' => 'sound', |
360
|
|
|
'method' => 'delete', |
361
|
|
|
'name' => $name, |
362
|
|
|
]; |
363
|
|
|
|
364
|
|
|
$response = $this->httpClient->createAndSendRequest($args); |
365
|
|
|
$data = $this->processResponse($response); |
366
|
|
|
|
367
|
|
|
return $data; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
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.