Skip to content

Commit 656eda4

Browse files
Sync Compatibility with psr/http-message 2.0 (#2)
* feature: Provide compatibility with psr/http-message 2.0 * feature: replace the HttpClient interface with ClientInterface * bugfix: forgot replacement --------- Co-authored-by: Brecht De Winne <brecht@optios.net>
1 parent 9378dbe commit 656eda4

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php-http/httplug": "^1.0 || ^2.0",
1818
"php-http/message": "^1.0",
1919
"php-http/client-implementation": "^1.0",
20-
"php-http/discovery": "^1.0"
20+
"php-http/discovery": "^1.0",
21+
"psr/http-client": "^1.0"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "^8.0 || ^9.0",

lib/SparkPost/SparkPost.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace SparkPost;
44

5-
use Http\Client\HttpClient;
6-
use Http\Client\HttpAsyncClient;
75
use Http\Discovery\MessageFactoryDiscovery;
86
use Http\Message\RequestFactory;
7+
use Psr\Http\Client\ClientInterface;
98
use Psr\Http\Message\RequestInterface;
109

1110
class SparkPost
@@ -16,7 +15,7 @@ class SparkPost
1615
private $version = '2.3.0';
1716

1817
/**
19-
* @var HttpClient|HttpAsyncClient used to make requests
18+
* @var ClientInterface used to make requests
2019
*/
2120
private $httpClient;
2221

@@ -53,7 +52,7 @@ class SparkPost
5352
/**
5453
* Sets up the SparkPost instance.
5554
*
56-
* @param HttpClient $httpClient - An httplug client or adapter
55+
* @param ClientInterface $httpClient - An httplug client or adapter
5756
* @param array $options - An array to overide default options or a string to be used as an API key
5857
*/
5958
public function __construct($httpClient, array $options)
@@ -277,14 +276,14 @@ public function getUrl($path, $params = [])
277276
/**
278277
* Sets $httpClient to be used for request.
279278
*
280-
* @param HttpClient|HttpAsyncClient $httpClient - the client to be used for request
279+
* @param ClientInterface $httpClient - the client to be used for request
281280
*
282281
* @return SparkPost
283282
*/
284283
public function setHttpClient($httpClient)
285284
{
286-
if (!($httpClient instanceof HttpAsyncClient || $httpClient instanceof HttpClient)) {
287-
throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s" or "%s"', HttpClient::class, HttpAsyncClient::class));
285+
if (!($httpClient instanceof ClientInterface)) {
286+
throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s"', ClientInterface::class));
288287
}
289288

290289
$this->httpClient = $httpClient;

lib/SparkPost/SparkPostResponse.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SparkPost;
44

5+
use Psr\Http\Message\MessageInterface;
56
use Psr\Http\Message\ResponseInterface as ResponseInterface;
67
use Psr\Http\Message\StreamInterface as StreamInterface;
78

@@ -39,84 +40,83 @@ public function getRequest()
3940
}
4041

4142
/**
42-
* Returns the body.
43+
* Gets the body in json format.
4344
*
44-
* @return array $body - the json decoded body from the http response
45+
* @return array Decoded body.
4546
*/
46-
public function getBody()
47-
{
48-
$body = $this->response->getBody();
49-
$body_string = $body->__toString();
50-
51-
$json = json_decode($body_string, true);
47+
public function getBodyAsJson() : array {
48+
return json_decode($this->getBody()->getContents(), true);
49+
}
5250

53-
return $json;
51+
public function getBody() : StreamInterface
52+
{
53+
return $this->response->getBody();
5454
}
5555

5656
/**
5757
* pass these down to the response given in the constructor.
5858
*/
59-
public function getProtocolVersion()
59+
public function getProtocolVersion() : string
6060
{
6161
return $this->response->getProtocolVersion();
6262
}
6363

64-
public function withProtocolVersion($version)
64+
public function withProtocolVersion($version) : MessageInterface
6565
{
6666
return $this->response->withProtocolVersion($version);
6767
}
6868

69-
public function getHeaders()
69+
public function getHeaders() : array
7070
{
7171
return $this->response->getHeaders();
7272
}
7373

74-
public function hasHeader($name)
74+
public function hasHeader($name) : bool
7575
{
7676
return $this->response->hasHeader($name);
7777
}
7878

79-
public function getHeader($name)
79+
public function getHeader($name) : array
8080
{
8181
return $this->response->getHeader($name);
8282
}
8383

84-
public function getHeaderLine($name)
84+
public function getHeaderLine($name) : string
8585
{
8686
return $this->response->getHeaderLine($name);
8787
}
8888

89-
public function withHeader($name, $value)
89+
public function withHeader($name, $value) : MessageInterface
9090
{
9191
return $this->response->withHeader($name, $value);
9292
}
9393

94-
public function withAddedHeader($name, $value)
94+
public function withAddedHeader($name, $value) : MessageInterface
9595
{
9696
return $this->response->withAddedHeader($name, $value);
9797
}
9898

99-
public function withoutHeader($name)
99+
public function withoutHeader($name) : MessageInterface
100100
{
101101
return $this->response->withoutHeader($name);
102102
}
103103

104-
public function withBody(StreamInterface $body)
104+
public function withBody(StreamInterface $body) : MessageInterface
105105
{
106106
return $this->response->withBody($body);
107107
}
108108

109-
public function getStatusCode()
109+
public function getStatusCode() : int
110110
{
111111
return $this->response->getStatusCode();
112112
}
113113

114-
public function withStatus($code, $reasonPhrase = '')
114+
public function withStatus($code, $reasonPhrase = '') : ResponseInterface
115115
{
116116
return $this->response->withStatus($code, $reasonPhrase);
117117
}
118118

119-
public function getReasonPhrase()
119+
public function getReasonPhrase() : string
120120
{
121121
return $this->response->getReasonPhrase();
122122
}

0 commit comments

Comments
 (0)