33namespace BulkGate \Sdk \Connection ;
44
55/**
6- * @author Lukáš Piják 2022 TOPefekt s.r.o.
6+ * @author Lukáš Piják 2025 TOPefekt s.r.o.
77 * @link https://www.bulkgate.com/
88 */
99
1010use BulkGate \Sdk \{ConnectionException , Utils \Strict };
11- use function implode ;
11+ use function array_key_exists , implode , restore_error_handler , set_error_handler ;
1212
1313class ConnectionStream implements Connection
1414{
@@ -25,7 +25,13 @@ class ConnectionStream implements Connection
2525 private string $ content_type ;
2626
2727
28- public function __construct (int $ application_id , string $ application_token , string $ api = 'https://portal.bulkgate.com/api/1.0/integration ' , string $ application_product = 'php-sdk ' , string $ content_type = 'application/json ' )
28+ public function __construct (
29+ int $ application_id ,
30+ string $ application_token ,
31+ string $ api = 'https://portal.bulkgate.com/api/1.0/integration ' ,
32+ string $ application_product = 'php-sdk ' ,
33+ string $ content_type = 'application/json '
34+ )
2935 {
3036 $ this ->application_id = $ application_id ;
3137 $ this ->application_token = $ application_token ;
@@ -40,42 +46,51 @@ public function __construct(int $application_id, string $application_token, stri
4046 */
4147 public function send (Request $ request ): Response
4248 {
43- [$ content_type , $ action , $ data ] = $ request ->encode ($ this ->content_type , [
44- 'application_id ' => $ this ->application_id ,
45- 'application_token ' => $ this ->application_token ,
46- 'application_product ' => $ this ->application_product
47- ]);
48-
49- $ context = stream_context_create (['http ' => [
50- 'method ' => 'POST ' ,
51- 'header ' => [
52- "Content-type: $ content_type "
53- ],
54- 'content ' => $ data ,
55- 'ignore_errors ' => true
56- ]]);
57-
58- $ connection = fopen ("$ this ->api / $ action " , 'r ' , false , $ context );
59-
60- if ($ connection )
61- {
62- try
63- {
64- $ response = (string ) stream_get_contents ($ connection );
65-
66- $ meta = stream_get_meta_data ($ connection );
67-
68- if (isset ($ meta ['wrapper_data ' ]))
69- {
70- return new Response (Helpers::parseContentType (implode ("\n" , $ meta ['wrapper_data ' ])), $ response );
71- }
72- }
73- finally
74- {
75- fclose ($ connection );
76- }
77- }
78-
79- throw new ConnectionException ("BulkGate server is unavailable - $ this ->api / $ action " );
49+ try
50+ {
51+ set_error_handler (function (int $ error_code , string $ error_message ): void { throw new ConnectionException ($ error_message , $ error_code ); });
52+
53+ [$ content_type , $ action , $ data ] = $ request ->encode ($ this ->content_type , [
54+ 'application_id ' => $ this ->application_id ,
55+ 'application_token ' => $ this ->application_token ,
56+ 'application_product ' => $ this ->application_product
57+ ]);
58+
59+ $ context = stream_context_create (['http ' => [
60+ 'method ' => 'POST ' ,
61+ 'header ' => [
62+ "Content-type: $ content_type "
63+ ],
64+ 'content ' => $ data ,
65+ 'ignore_errors ' => true
66+ ]]);
67+
68+ $ connection = fopen ("$ this ->api / $ action " , 'r ' , false , $ context );
69+
70+ if ($ connection )
71+ {
72+ try
73+ {
74+ $ response = (string ) stream_get_contents ($ connection );
75+
76+ $ meta = stream_get_meta_data ($ connection );
77+
78+ if (array_key_exists ('wrapper_data ' , $ meta ))
79+ {
80+ return new Response (Helpers::parseContentType (implode ("\n" , $ meta ['wrapper_data ' ])), $ response , Helpers::parseHttpCode ($ meta ['wrapper_data ' ][0 ] ?? null ));
81+ }
82+ }
83+ finally
84+ {
85+ fclose ($ connection );
86+ }
87+ }
88+ }
89+ finally
90+ {
91+ restore_error_handler ();
92+ }
93+
94+ throw new ConnectionException ("BulkGate server is unavailable - $ this ->api / $ action " );
8095 }
8196}
0 commit comments