Skip to content

Commit 2711086

Browse files
authored
Merge pull request smsapi#132 from smsapi/uri-with-port
Uri with port
2 parents 188cd39 + 6d4b620 commit 2711086

13 files changed

Lines changed: 286 additions & 29 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55

6+
## [Unreleased]
7+
### Added
8+
- service url scheme and host validation
9+
- service url with port support
10+
11+
### Changed
12+
- PSR-18 HTTP client related exceptions namespace moved
13+
614
## [3.0.10] - 2024-01-16
715
### Added
816
- PHP-8.3 support

src/Curl/Exception/ClientException.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,14 @@
44

55
namespace Smsapi\Client\Curl\Exception;
66

7-
use Exception;
8-
use Psr\Http\Client\ClientExceptionInterface;
9-
use Psr\Http\Message\RequestInterface;
7+
use Smsapi\Client\Infrastructure\HttpClient\ClientException as HttpClientException;
108

119
/**
1210
* @api
11+
* @deprecated
12+
* @see HttpClientException
1313
*/
14-
class ClientException extends Exception implements ClientExceptionInterface
14+
class ClientException extends HttpClientException
1515
{
16-
private $request;
1716

18-
public static function withRequest(string $message, RequestInterface $request): self
19-
{
20-
$exception = new self($message);
21-
$exception->request = $request;
22-
23-
return $exception;
24-
}
25-
26-
public function getRequest(): RequestInterface
27-
{
28-
return $this->request;
29-
}
3017
}

src/Curl/Exception/NetworkException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Smsapi\Client\Curl\Exception;
66

7-
use Psr\Http\Client\NetworkExceptionInterface;
7+
use Smsapi\Client\Infrastructure\HttpClient\NetworkException as HttpClientNetworkException;
88

99
/**
1010
* @api
11+
* @deprecated
12+
* @see HttpClientNetworkException
1113
*/
12-
class NetworkException extends ClientException implements NetworkExceptionInterface
14+
class NetworkException extends HttpClientNetworkException
1315
{
1416

1517
}

src/Curl/Exception/RequestException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Smsapi\Client\Curl\Exception;
66

7-
use Psr\Http\Client\RequestExceptionInterface;
7+
use Smsapi\Client\Infrastructure\HttpClient\RequestException as HttpClientRequestException;
88

99
/**
1010
* @api
11+
* @deprecated
12+
* @see HttpClientRequestException
1113
*/
12-
class RequestException extends ClientException implements RequestExceptionInterface
14+
class RequestException extends HttpClientRequestException
1315
{
1416

1517
}

src/Curl/HttpClient.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Psr\Http\Client\ClientInterface;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
11-
use Smsapi\Client\Curl\Exception\NetworkException;
12-
use Smsapi\Client\Curl\Exception\RequestException;
11+
use Smsapi\Client\Infrastructure\HttpClient\NetworkException;
12+
use Smsapi\Client\Infrastructure\HttpClient\RequestException;
1313

1414
/**
1515
* @internal
@@ -34,7 +34,13 @@ public function sendRequest(RequestInterface $request): ResponseInterface
3434

3535
private function prepareRequestHttpClient(RequestInterface $request)
3636
{
37-
$url = sprintf("%s://%s%s", $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getRequestTarget());
37+
$url = strtr("{scheme}://{host}{port}{path}", [
38+
'{scheme}' => $request->getUri()->getScheme(),
39+
'{host}' => $request->getUri()->getHost(),
40+
'{port}' => $request->getUri()->getPort() ? ':' . $request->getUri()->getPort() : '',
41+
'{path}' => $request->getRequestTarget()
42+
]);
43+
3844
$httpClient = curl_init($url);
3945

4046
if ($httpClient === false) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smsapi\Client\Infrastructure\HttpClient;
6+
7+
use Exception;
8+
use Psr\Http\Client\ClientExceptionInterface;
9+
use Psr\Http\Message\RequestInterface;
10+
11+
/**
12+
* @api
13+
*/
14+
class ClientException extends Exception implements ClientExceptionInterface
15+
{
16+
private $request;
17+
18+
public static function withRequest(string $message, RequestInterface $request): self
19+
{
20+
$exception = new static($message);
21+
$exception->request = $request;
22+
23+
return $exception;
24+
}
25+
26+
public function getRequest(): RequestInterface
27+
{
28+
return $this->request;
29+
}
30+
}

src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Http\Client\ClientInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Smsapi\Client\Infrastructure\HttpClient\RequestException;
1011

1112
/**
1213
* @internal
@@ -33,14 +34,20 @@ private function prependBaseUri(RequestInterface $request): RequestInterface
3334
{
3435
$uri = $request->getUri();
3536

37+
if (!filter_var($this->baseUri, FILTER_VALIDATE_URL)) {
38+
throw RequestException::withRequest("Invalid Base URI", $request);
39+
}
40+
3641
$baseUriParts = parse_url($this->baseUri);
3742

3843
$scheme = $baseUriParts['scheme'] ?? '';
3944
$host = $baseUriParts['host'] ?? '';
45+
$port = $baseUriParts['port'] ?? null;
4046
$basePath = $baseUriParts['path'] ?? '';
4147
$basePath = rtrim($basePath, '/');
4248

4349
$uri = $uri->withPath($basePath . '/' . $uri->getPath());
50+
$uri = $uri->withPort($port);
4451
$uri = $uri->withHost($host);
4552
$uri = $uri->withScheme($scheme);
4653

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smsapi\Client\Infrastructure\HttpClient;
6+
7+
use Psr\Http\Client\NetworkExceptionInterface;
8+
9+
/**
10+
* @api
11+
*/
12+
class NetworkException extends ClientException implements NetworkExceptionInterface
13+
{
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smsapi\Client\Infrastructure\HttpClient;
6+
7+
use Psr\Http\Client\RequestExceptionInterface;
8+
9+
/**
10+
* @api
11+
*/
12+
class RequestException extends ClientException implements RequestExceptionInterface
13+
{
14+
15+
}

src/SmsapiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
interface SmsapiClient extends LoggerAwareInterface
1414
{
15-
const VERSION = '3.0.10';
15+
const VERSION = 'Unreleased';
1616

1717
public function smsapiPlService(string $apiToken): SmsapiPlService;
1818

0 commit comments

Comments
 (0)