6
6
use Psr \Log \LoggerAwareTrait ;
7
7
use Psr \Log \LoggerInterface ;
8
8
use Psr \Log \LogLevel ;
9
+ use GuzzleHttp \Psr7 \Request ;
10
+ use GuzzleHttp \Promise \PromiseInterface ;
9
11
10
12
class Pusher implements LoggerAwareInterface, PusherInterface
11
13
{
@@ -14,7 +16,7 @@ class Pusher implements LoggerAwareInterface, PusherInterface
14
16
/**
15
17
* @var string Version
16
18
*/
17
- public static $ VERSION = '6.0.1 ' ;
19
+ public static $ VERSION = '6.1.0 ' ;
18
20
19
21
/**
20
22
* @var null|PusherCrypto
@@ -326,8 +328,8 @@ public static function array_implode($glue, $separator, $array)
326
328
}
327
329
328
330
/**
329
- * Trigger an event by providing event name and payload.
330
- * Optionally provide a socket ID to exclude a client (most likely the sender) .
331
+ * Helper function to prepare trigger request. Takes the same
332
+ * parameters as the public trigger functions .
331
333
*
332
334
* @param array|string $channels A channel name or an array of channel names to publish the event on.
333
335
* @param string $event
@@ -340,7 +342,7 @@ public static function array_implode($glue, $separator, $array)
340
342
* @throws GuzzleException
341
343
*
342
344
*/
343
- public function trigger ($ channels , $ event , $ data , $ params = array (), $ already_encoded = false ) : object
345
+ public function make_request ($ channels , $ event , $ data , $ params = array (), $ already_encoded = false ) : Request
344
346
{
345
347
if (is_string ($ channels ) === true ) {
346
348
$ channels = array ($ channels );
@@ -355,6 +357,7 @@ public function trigger($channels, $event, $data, $params = array(), $already_en
355
357
foreach ($ channels as $ chan ) {
356
358
if (PusherCrypto::is_encrypted_channel ($ chan )) {
357
359
$ has_encrypted_channel = true ;
360
+ break ;
358
361
}
359
362
}
360
363
@@ -400,10 +403,33 @@ public function trigger($channels, $event, $data, $params = array(), $already_en
400
403
'X-Pusher-Library ' => 'pusher-http-php ' .self ::$ VERSION
401
404
];
402
405
403
- $ response = $ this ->client ->post ($ path , [
404
- 'query ' => array_merge ($ signature , $ query_params ),
405
- 'headers ' => $ headers ,
406
- 'body ' => $ post_value ,
406
+ $ params = array_merge ($ signature , $ query_params );
407
+ $ query_string = self ::array_implode ('= ' , '& ' , $ params );
408
+ $ full_path = $ path ."? " .$ query_string ;
409
+ $ request = new Request ('POST ' , $ full_path , $ headers , $ post_value );
410
+
411
+ return $ request ;
412
+ }
413
+
414
+ /**
415
+ * Trigger an event by providing event name and payload.
416
+ * Optionally provide a socket ID to exclude a client (most likely the sender).
417
+ *
418
+ * @param array|string $channels A channel name or an array of channel names to publish the event on.
419
+ * @param string $event
420
+ * @param mixed $data Event data
421
+ * @param array $params [optional]
422
+ * @param bool $already_encoded [optional]
423
+ *
424
+ * @throws PusherException Throws PusherException if $channels is an array of size 101 or above or $socket_id is invalid
425
+ * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
426
+ * @throws GuzzleException
427
+ *
428
+ */
429
+ public function trigger ($ channels , $ event , $ data , $ params = array (), $ already_encoded = false ) : object {
430
+ $ request = $ this ->make_request ($ channels , $ event , $ data , $ params , $ already_encoded );
431
+
432
+ $ response = $ this ->client ->send ($ request , [
407
433
'http_errors ' => false ,
408
434
'base_uri ' => $ this ->channels_url_prefix ()
409
435
]);
@@ -425,16 +451,53 @@ public function trigger($channels, $event, $data, $params = array(), $already_en
425
451
}
426
452
427
453
/**
428
- * Trigger multiple events at the same time.
454
+ * Asynchronously trigger an event by providing event name and payload.
455
+ * Optionally provide a socket ID to exclude a client (most likely the sender).
456
+ *
457
+ * @param array|string $channels A channel name or an array of channel names to publish the event on.
458
+ * @param string $event
459
+ * @param mixed $data Event data
460
+ * @param array $params [optional]
461
+ * @param bool $already_encoded [optional]
462
+ *
463
+ */
464
+ public function triggerAsync ($ channels , $ event , $ data , $ params = array (), $ already_encoded = false ) : PromiseInterface
465
+ {
466
+ $ request = $ this ->make_request ($ channels , $ event , $ data , $ params , $ already_encoded );
467
+
468
+ $ promise = $ this ->client ->sendAsync ($ request , [
469
+ 'http_errors ' => false ,
470
+ 'base_uri ' => $ this ->channels_url_prefix ()
471
+ ])->then (function ($ response ) {
472
+ $ status = $ response ->getStatusCode ();
473
+
474
+ if ($ status !== 200 ) {
475
+ $ body = (string ) $ response ->getBody ();
476
+ throw new ApiErrorException ($ body , $ status );
477
+ }
478
+
479
+ $ result = json_decode ($ response ->getBody ());
480
+
481
+ if (property_exists ($ result , 'channels ' )) {
482
+ $ result ->channels = get_object_vars ($ result ->channels );
483
+ }
484
+
485
+ return $ result ;
486
+ });
487
+
488
+ return $ promise ;
489
+ }
490
+
491
+ /**
492
+ * Helper function to prepare batch trigger request. Takes the same * parameters as the public batch trigger functions.
429
493
*
430
494
* @param array $batch [optional] An array of events to send
431
495
* @param bool $already_encoded [optional]
432
496
*
433
497
* @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
434
- * @throws GuzzleException
435
498
*
436
- */
437
- public function triggerBatch ($ batch = array (), $ already_encoded = false ) : object
499
+ ** /
500
+ public function make_batch_request ($ batch = array (), $ already_encoded = false ) : Request
438
501
{
439
502
foreach ($ batch as $ key => $ event ) {
440
503
$ this ->validate_channel ($ event ['channel ' ]);
@@ -471,10 +534,29 @@ public function triggerBatch($batch = array(), $already_encoded = false) : objec
471
534
'X-Pusher-Library ' => 'pusher-http-php ' .self ::$ VERSION
472
535
];
473
536
474
- $ response = $ this ->client ->post ($ path , [
475
- 'query ' => array_merge ($ query_params , $ signature ),
476
- 'body ' => $ post_value ,
477
- 'headers ' => $ headers ,
537
+ $ params = array_merge ($ signature , $ query_params );
538
+ $ query_string = self ::array_implode ('= ' , '& ' , $ params );
539
+ $ full_path = $ path ."? " .$ query_string ;
540
+ $ request = new Request ('POST ' , $ full_path , $ headers , $ post_value );
541
+
542
+ return $ request ;
543
+ }
544
+
545
+ /**
546
+ * Trigger multiple events at the same time.
547
+ *
548
+ * @param array $batch [optional] An array of events to send
549
+ * @param bool $already_encoded [optional]
550
+ *
551
+ * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
552
+ * @throws GuzzleException
553
+ *
554
+ */
555
+ public function triggerBatch ($ batch = array (), $ already_encoded = false ) : object
556
+ {
557
+ $ request = $ this ->make_batch_request ($ batch , $ already_encoded );
558
+
559
+ $ response = $ this ->client ->send ($ request , [
478
560
'http_errors ' => false ,
479
561
'base_uri ' => $ this ->channels_url_prefix ()
480
562
]);
@@ -486,7 +568,50 @@ public function triggerBatch($batch = array(), $already_encoded = false) : objec
486
568
throw new ApiErrorException ($ body , $ status );
487
569
}
488
570
489
- return json_decode ($ response ->getBody ());
571
+ $ result = json_decode ($ response ->getBody ());
572
+
573
+ if (property_exists ($ result , 'channels ' )) {
574
+ $ result ->channels = get_object_vars ($ result ->channels );
575
+ }
576
+
577
+ return $ result ;
578
+ }
579
+
580
+ /**
581
+ * Asynchronously trigger multiple events at the same time.
582
+ *
583
+ * @param array $batch [optional] An array of events to send
584
+ * @param bool $already_encoded [optional]
585
+ *
586
+ * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
587
+ *
588
+ */
589
+ public function triggerBatchAsync ($ batch = array (), $ already_encoded = false ) : PromiseInterface
590
+ {
591
+ $ request = $ this ->make_batch_request ($ batch , $ already_encoded );
592
+
593
+ $ promise = $ this ->client ->sendAsync ($ request , [
594
+ 'http_errors ' => false ,
595
+ 'base_uri ' => $ this ->channels_url_prefix ()
596
+ ])->then (function ($ response ) {
597
+ $ status = $ response ->getStatusCode ();
598
+
599
+ if ($ status !== 200 ) {
600
+ $ body = (string ) $ response ->getBody ();
601
+ throw new ApiErrorException ($ body , $ status );
602
+ }
603
+
604
+ $ result = json_decode ($ response ->getBody ());
605
+
606
+ if (property_exists ($ result , 'channels ' )) {
607
+ $ result ->channels = get_object_vars ($ result ->channels );
608
+ }
609
+
610
+ return $ result ;
611
+ });
612
+
613
+ return $ promise ;
614
+
490
615
}
491
616
492
617
/**
0 commit comments