5
5
use Broadcastt \Exception \InvalidArgumentException ;
6
6
use Broadcastt \Exception \InvalidChannelNameException ;
7
7
use Broadcastt \Exception \InvalidDataException ;
8
+ use Broadcastt \Exception \InvalidHostException ;
8
9
use Broadcastt \Exception \InvalidSocketIdException ;
10
+ use Broadcastt \Exception \JsonEncodeException ;
9
11
use Broadcastt \Exception \TooManyChannelsException ;
10
- use Broadcastt \Exception \InvalidHostException ;
11
12
use GuzzleHttp \Client ;
12
13
use GuzzleHttp \ClientInterface ;
13
14
use GuzzleHttp \Exception \GuzzleException ;
14
15
use GuzzleHttp \Psr7 \Request ;
15
- use GuzzleHttp \Psr7 \Response ;
16
- use function GuzzleHttp \Psr7 \stream_for ;
17
16
use GuzzleHttp \Psr7 \Uri ;
18
17
use Psr \Http \Message \RequestInterface ;
18
+ use Psr \Http \Message \ResponseInterface ;
19
19
use Psr \Http \Message \UriInterface ;
20
20
use Psr \Log \LoggerAwareInterface ;
21
21
use Psr \Log \LoggerAwareTrait ;
22
22
use Psr \Log \LogLevel ;
23
+ use function GuzzleHttp \Psr7 \stream_for ;
23
24
24
25
/**
25
26
* Class BroadcasttClient
27
+ *
26
28
* @package Broadcastt
27
29
*
28
30
* @property-read int $appId Id of your application
@@ -74,7 +76,6 @@ class BroadcasttClient implements LoggerAwareInterface
74
76
'timeout ' => null ,
75
77
];
76
78
77
-
78
79
/**
79
80
* Initializes a new Broadcastt instance with key, secret and ID of an app.
80
81
*
@@ -101,6 +102,7 @@ public function __construct($appId, $appKey, $appSecret, $appCluster = 'eu')
101
102
* Clients can be instantiated from a URI. For example: "http://key:[email protected] /apps/{appId}"
102
103
*
103
104
* @param string|UriInterface $uri
105
+ *
104
106
* @return BroadcasttClient
105
107
*/
106
108
public static function fromUri ($ uri )
@@ -126,7 +128,7 @@ public static function fromUri($uri)
126
128
throw new InvalidArgumentException ('Secret part of user info is missing from URI ' );
127
129
}
128
130
129
- list ( $ appKey , $ appSecret) = $ userInfo ;
131
+ [ $ appKey , $ appSecret] = $ userInfo ;
130
132
131
133
$ client = new BroadcasttClient ($ appId , $ appKey , $ appSecret );
132
134
$ client ->scheme = $ uri ->getScheme ();
@@ -233,7 +235,7 @@ private function buildRequest($domain, $path, $requestMethod = 'GET', $queryPara
233
235
*
234
236
* @param RequestInterface $request
235
237
*
236
- * @return Response
238
+ * @return ResponseInterface
237
239
* @throws GuzzleException
238
240
*/
239
241
private function sendRequest ($ request )
@@ -275,6 +277,7 @@ private function buildUri()
275
277
* Check if the status code indicates the request was successful.
276
278
*
277
279
* @param $status
280
+ *
278
281
* @return bool
279
282
*/
280
283
private function isSuccessStatusCode ($ status )
@@ -345,7 +348,8 @@ public static function httpBuildQuery($array)
345
348
* @param bool $jsonEncoded [optional]
346
349
*
347
350
* @return bool
348
- * invalid
351
+ * @throws GuzzleException
352
+ * @throws JsonEncodeException on JSON encode failure.
349
353
*/
350
354
public function trigger ($ channels , $ name , $ data , $ socketId = null , $ jsonEncoded = false )
351
355
{
@@ -356,33 +360,28 @@ public function trigger($channels, $name, $data, $socketId = null, $jsonEncoded
356
360
$ this ->validateChannels ($ channels );
357
361
$ this ->validateSocketId ($ socketId );
358
362
363
+ $ jsonData = $ data ;
359
364
if (!$ jsonEncoded ) {
360
- $ data = json_encode ($ data );
365
+ $ jsonData = json_encode ($ data );
361
366
362
- // json_encode might return false on failure
363
- if (!$ data ) {
364
- $ this ->log ('Failed to perform json_encode on the the provided data: {error} ' , [
365
- 'error ' => $ data ,
366
- ], LogLevel::ERROR );
367
+ // json_encode returns false on failure
368
+ if ($ jsonData === false ) {
369
+ throw new JsonEncodeException ($ data , json_last_error_msg (), json_last_error ());
367
370
}
368
371
}
369
372
370
373
$ postParams = [];
371
374
$ postParams ['name ' ] = $ name ;
372
- $ postParams ['data ' ] = $ data ;
375
+ $ postParams ['data ' ] = $ jsonData ;
373
376
$ postParams ['channels ' ] = $ channels ;
374
377
375
378
if ($ socketId !== null ) {
376
379
$ postParams ['socket_id ' ] = $ socketId ;
377
380
}
378
381
379
- try {
380
- $ response = $ this ->post ('/event ' , [], $ postParams );
382
+ $ response = $ this ->post ('/event ' , [], $ postParams );
381
383
382
- return $ this ->isSuccessStatusCode ($ response ->getStatusCode ());
383
- } catch (GuzzleException $ e ) {
384
- return false ;
385
- }
384
+ return $ this ->isSuccessStatusCode ($ response ->getStatusCode ());
386
385
}
387
386
388
387
/**
@@ -392,6 +391,8 @@ public function trigger($channels, $name, $data, $socketId = null, $jsonEncoded
392
391
* @param bool $jsonEncoded [optional] Defines if the data is already encoded
393
392
*
394
393
* @return bool
394
+ * @throws GuzzleException
395
+ * @throws JsonEncodeException on JSON encode failure.
395
396
*/
396
397
public function triggerBatch ($ batch = [], $ jsonEncoded = false )
397
398
{
@@ -404,21 +405,23 @@ public function triggerBatch($batch = [], $jsonEncoded = false)
404
405
}
405
406
406
407
if (!$ jsonEncoded ) {
407
- $ batch [$ key ]['data ' ] = json_encode ($ event ['data ' ]);
408
+ $ jsonData = json_encode ($ event ['data ' ]);
409
+
410
+ // json_encode returns false on failure
411
+ if ($ jsonData === false ) {
412
+ throw new JsonEncodeException ($ event ['data ' ], json_last_error_msg (), json_last_error ());
413
+ }
414
+
415
+ $ batch [$ key ]['data ' ] = $ jsonData ;
408
416
}
409
417
}
410
418
411
419
$ postParams = [];
412
420
$ postParams ['batch ' ] = $ batch ;
413
421
422
+ $ response = $ this ->post ('/events ' , [], $ postParams );
414
423
415
- try {
416
- $ response = $ this ->post ('/events ' , [], $ postParams );
417
-
418
- return $ this ->isSuccessStatusCode ($ response ->getStatusCode ());
419
- } catch (GuzzleException $ e ) {
420
- return false ;
421
- }
424
+ return $ this ->isSuccessStatusCode ($ response ->getStatusCode ());
422
425
}
423
426
424
427
/**
@@ -429,7 +432,7 @@ public function triggerBatch($batch = [], $jsonEncoded = false)
429
432
* @param array $queryParams API query params (see https://broadcastt.xyz/docs/References-‐-Rest-API)
430
433
* @param array $postParams API post params (see https://broadcastt.xyz/docs/References-‐-Rest-API)
431
434
*
432
- * @return Response
435
+ * @return ResponseInterface
433
436
* @throws GuzzleException
434
437
*/
435
438
private function post ($ path , $ queryParams = [], $ postParams = [])
@@ -453,7 +456,7 @@ private function post($path, $queryParams = [], $postParams = [])
453
456
* @param string $path Path excluding /apps/{appId}
454
457
* @param array $queryParams API query params (see https://broadcastt.xyz/docs/References-‐-Rest-API)
455
458
*
456
- * @return Response See Broadcastt API docs
459
+ * @return ResponseInterface See Broadcastt API docs
457
460
* @throws GuzzleException
458
461
*/
459
462
public function get ($ path , $ queryParams = [])
@@ -568,5 +571,4 @@ public function __set($name, $value)
568
571
$ this ->modifiers [$ name ] = $ value ;
569
572
}
570
573
}
571
-
572
574
}
0 commit comments