Skip to content

Commit f841e3a

Browse files
committed
improve connection errors
1 parent 7314255 commit f841e3a

File tree

6 files changed

+205
-160
lines changed

6 files changed

+205
-160
lines changed

src/Configurator/SmsCountryConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function configure(Base $message): void
5757
{
5858
[, $prefix] = $match;
5959

60-
$iso = PrefixMap::PREFIX_TO_ISO[$prefix] ?? null;
60+
$iso = PrefixMap::PREFIX_TO_ISO[$prefix];
6161

6262
if ($iso !== null && isset($this->profile[$iso]))
6363
{

src/Connection/ConnectionStream.php

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace 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

1010
use BulkGate\Sdk\{ConnectionException, Utils\Strict};
11-
use function implode;
11+
use function array_key_exists, implode, restore_error_handler, set_error_handler;
1212

1313
class 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
}

src/Connection/Helpers.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace 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

@@ -12,19 +12,37 @@
1212

1313
class Helpers
1414
{
15-
use Strict;
15+
use Strict;
1616

17+
public static function parseContentType(string $header): ?string
18+
{
19+
$header = mb_strtolower($header);
1720

18-
public static function parseContentType(string $header): ?string
19-
{
20-
$header = strtolower($header);
21+
if (preg_match('~content-type:\s([^\n;]+)~', mb_strtolower($header), $m))
22+
{
23+
[, $content_type] = $m;
2124

22-
if (preg_match('~content-type:\s([^\n;]+)~', mb_strtolower($header), $m))
23-
{
24-
[, $content_type] = $m;
25+
return $content_type;
26+
}
27+
return null;
28+
}
2529

26-
return $content_type;
27-
}
28-
return null;
29-
}
30+
31+
public static function parseHttpCode(?string $header): ?int
32+
{
33+
if ($header === null)
34+
{
35+
return null;
36+
}
37+
38+
$header = mb_strtolower($header);
39+
40+
if (preg_match('~\s(\d{3})~', mb_strtolower($header), $m))
41+
{
42+
[, $code] = $m;
43+
44+
return (int) $code;
45+
}
46+
return null;
47+
}
3048
}

0 commit comments

Comments
 (0)