@@ -223,6 +223,20 @@ private function validate_socket_id(string $socket_id): void
223
223
}
224
224
}
225
225
226
+ /**
227
+ * Ensure an user id is valid based on our spec.
228
+ *
229
+ * @param string $user_id The user id to validate
230
+ *
231
+ * @throws PusherException If $user_id is invalid
232
+ */
233
+ private function validate_user_id (string $ user_id ): void
234
+ {
235
+ if ($ user_id === null || empty ($ user_id )) {
236
+ throw new PusherException ('Invalid user id ' . $ user_id );
237
+ }
238
+ }
239
+
226
240
/**
227
241
* Utility function used to generate signing headers
228
242
*
@@ -646,6 +660,40 @@ public function triggerBatchAsync(array $batch = [], bool $already_encoded = fal
646
660
return $ promise ;
647
661
}
648
662
663
+ /**
664
+ * Terminates all connections established by the user with the given user id.
665
+ *
666
+ * @param string $userId
667
+ *
668
+ * @throws PusherException If $userId is invalid
669
+ * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
670
+ *
671
+ * @return object response body
672
+ *
673
+ */
674
+ public function terminateUserConnections (string $ userId ): object
675
+ {
676
+ $ this ->validate_user_id ($ userId );
677
+ return $ this ->post ("/users/ " . $ userId . "/terminate_connections " , "{} " );
678
+ }
679
+
680
+ /**
681
+ * Asynchronous request to terminates all connections established by the user with the given user id.
682
+ *
683
+ * @param string $userId
684
+ *
685
+ * @throws PusherException If $userId is invalid
686
+ *
687
+ * @return PromiseInterface promise wrapping response body
688
+ *
689
+ */
690
+ public function terminateUserConnectionsAsync (string $ userId ): PromiseInterface
691
+ {
692
+ $ this ->validate_user_id ($ userId );
693
+ return $ this ->postAsync ("/users/ " . $ userId . "/terminate_connections " , "{} " );
694
+ }
695
+
696
+
649
697
/**
650
698
* Fetch channel information for a specific channel.
651
699
*
@@ -768,6 +816,106 @@ public function get(string $path, array $params = [], $associative = false)
768
816
return $ body ;
769
817
}
770
818
819
+ /**
820
+ * POST arbitrary REST API resource using a synchronous http client.
821
+ * All request signing is handled automatically.
822
+ *
823
+ * @param string $path Path excluding /apps/APP_ID
824
+ * @param mixed $body Request payload (see http://pusher.com/docs/rest_api)
825
+ * @param array $params API params (see http://pusher.com/docs/rest_api)
826
+ *
827
+ * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
828
+ * @throws GuzzleException
829
+ * @throws PusherException
830
+ *
831
+ * @return mixed Post response body
832
+ */
833
+ public function post (string $ path , $ body , array $ params = [])
834
+ {
835
+ $ path = $ this ->settings ['base_path ' ] . $ path ;
836
+
837
+ $ params ['body_md5 ' ] = md5 ($ body );
838
+
839
+ $ params_with_signature = $ this ->sign ($ path , 'POST ' , $ params );
840
+
841
+ $ headers = [
842
+ 'Content-Type ' => 'application/json ' ,
843
+ 'X-Pusher-Library ' => 'pusher-http-php ' . self ::$ VERSION
844
+ ];
845
+
846
+ $ response = $ this ->client ->request ('POST ' , $ path , [
847
+ 'query ' => $ params_with_signature ,
848
+ 'body ' => $ body ,
849
+ 'http_errors ' => false ,
850
+ 'headers ' => $ headers ,
851
+ 'base_uri ' => $ this ->channels_url_prefix ()
852
+ ]);
853
+
854
+ $ status = $ response ->getStatusCode ();
855
+
856
+ if ($ status !== 200 ) {
857
+ $ body = (string ) $ response ->getBody ();
858
+ throw new ApiErrorException ($ body , $ status );
859
+ }
860
+
861
+ try {
862
+ $ response_body = json_decode ($ response ->getBody (), false , 512 , JSON_THROW_ON_ERROR );
863
+ } catch (\JsonException $ e ) {
864
+ throw new PusherException ('Data decoding error. ' );
865
+ }
866
+
867
+ return $ response_body ;
868
+ }
869
+
870
+ /**
871
+ * Asynchronously POST arbitrary REST API resource using a synchronous http client.
872
+ * All request signing is handled automatically.
873
+ *
874
+ * @param string $path Path excluding /apps/APP_ID
875
+ * @param mixed $body Request payload (see http://pusher.com/docs/rest_api)
876
+ * @param array $params API params (see http://pusher.com/docs/rest_api)
877
+ *
878
+ * @return PromiseInterface Promise wrapping POST response body
879
+ */
880
+ public function postAsync (string $ path , $ body , array $ params = []): PromiseInterface
881
+ {
882
+ $ path = $ this ->settings ['base_path ' ] . $ path ;
883
+
884
+ $ params ['body_md5 ' ] = md5 ($ body );
885
+
886
+ $ params_with_signature = $ this ->sign ($ path , 'POST ' , $ params );
887
+
888
+ $ headers = [
889
+ 'Content-Type ' => 'application/json ' ,
890
+ 'X-Pusher-Library ' => 'pusher-http-php ' . self ::$ VERSION
891
+ ];
892
+
893
+ return $ this ->client ->requestAsync ('POST ' , $ path , [
894
+ 'query ' => $ params_with_signature ,
895
+ 'body ' => $ body ,
896
+ 'http_errors ' => false ,
897
+ 'headers ' => $ headers ,
898
+ 'base_uri ' => $ this ->channels_url_prefix ()
899
+ ])->then (function ($ response ) {
900
+ $ status = $ response ->getStatusCode ();
901
+
902
+ if ($ status !== 200 ) {
903
+ $ body = (string ) $ response ->getBody ();
904
+ throw new ApiErrorException ($ body , $ status );
905
+ }
906
+
907
+ try {
908
+ $ response_body = json_decode ($ response ->getBody (), false , 512 , JSON_THROW_ON_ERROR );
909
+ } catch (\JsonException $ e ) {
910
+ throw new PusherException ('Data decoding error. ' );
911
+ }
912
+
913
+ return $ response_body ;
914
+ }, function (ConnectExpcetion $ e ) {
915
+ throw new ApiErrorException ($ e ->getMessage ());
916
+ });
917
+ }
918
+
771
919
/**
772
920
* Creates a socket signature.
773
921
*
0 commit comments