Skip to content

Commit b95e4b7

Browse files
committed
Remove php-http/* in favor of psr/http-* packages
1 parent d8d3d41 commit b95e4b7

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
"require": {
99
"php": "^8.0",
1010
"ext-json": "*",
11-
"php-http/discovery": "^1.0",
12-
"php-http/client-implementation": "^1.0",
11+
"psr/http-client": "^1.0",
12+
"psr/http-factory": "^1.0",
1313
"psr/log": "^1.0 || ^2.0 || ^3.0"
1414
},
1515
"require-dev": {
16-
"php-http/curl-client": "^1.0",
16+
"php-http/curl-client": "^2.3",
1717
"php-http/message": "^1.0",
18-
"guzzlehttp/psr7": "^1.0",
18+
"nyholm/psr7": "^1.8",
19+
"guzzlehttp/psr7": "^2.6",
1920
"monolog/monolog": "^2.0",
2021
"roave/security-advisories": "dev-latest",
2122
"phpunit/phpunit": "^9.0 || ^10.0",

src/Client.php

+44-18
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Stefna\Mailchimp;
44

5-
use Http\Client\HttpClient;
6-
use Http\Discovery\MessageFactoryDiscovery;
7-
use Http\Message\MessageFactory;
85
use InvalidArgumentException;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Http\Message\RequestFactoryInterface;
98
use Psr\Http\Message\RequestInterface;
109
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamFactoryInterface;
11+
use Psr\Http\Message\UriFactoryInterface;
12+
use Psr\Http\Message\UriInterface;
1113
use Psr\Log\LoggerInterface;
1214
use RuntimeException;
1315
use Stefna\Mailchimp\Api\Campaigns\Campaigns as CampaignsApi;
@@ -18,25 +20,30 @@
1820
class Client
1921
{
2022
private const DEFAULT_ENDPOINT = 'https://<dc>.api.mailchimp.com/3.0';
21-
2223
protected ?LoggerInterface $logger = null;
2324
protected ?ResponseInterface $lastResponse;
2425
protected ?RequestInterface $lastRequest;
25-
protected MessageFactory $messageFactory;
26-
protected HttpClient $httpClient;
26+
protected RequestFactoryInterface $messageFactory;
27+
protected ClientInterface $httpClient;
28+
protected UriFactoryInterface $uriFactory;
29+
protected StreamFactoryInterface $streamFactory;
2730
protected string $apiKey;
2831
protected string $apiEndpoint = '';
2932

3033
public function __construct(
31-
HttpClient $httpClient,
34+
ClientInterface $httpClient,
3235
string $apiKey,
36+
RequestFactoryInterface $messageFactory,
37+
UriFactoryInterface $uriFactory,
38+
StreamFactoryInterface $streamFactory,
3339
?string $apiEndpoint = null,
34-
?MessageFactory $messageFactory = null
3540
) {
3641
$this->httpClient = $httpClient;
3742
$this->apiKey = $apiKey;
43+
$this->messageFactory = $messageFactory;
44+
$this->uriFactory = $uriFactory;
45+
$this->streamFactory = $streamFactory;
3846
$this->apiEndpoint = $apiEndpoint ?: $this->createApiEndpoint($apiKey);
39-
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
4047
}
4148

4249
public function lists(): ListsApi
@@ -64,7 +71,7 @@ public function setLogger(LoggerInterface $logger): void
6471
$this->logger = $logger;
6572
}
6673

67-
public function getHttpClient(): HttpClient
74+
public function getHttpClient(): ClientInterface
6875
{
6976
return $this->httpClient;
7077
}
@@ -76,7 +83,7 @@ public function getHttpClient(): HttpClient
7683
public function get(string $path, array $args = []): array
7784
{
7885
/** @var array<string, mixed> */
79-
return $this->request($this->messageFactory->createRequest(
86+
return $this->request($this->createRequest(
8087
'GET',
8188
$this->createUrl($path, $args),
8289
$this->getDefaultHeaders()
@@ -88,7 +95,7 @@ public function get(string $path, array $args = []): array
8895
*/
8996
public function delete(string $path, array $args = []): bool
9097
{
91-
return $this->request($this->messageFactory->createRequest(
98+
return $this->request($this->createRequest(
9299
'DELETE',
93100
$this->createUrl($path, $args),
94101
$this->getDefaultHeaders()
@@ -102,7 +109,7 @@ public function delete(string $path, array $args = []): bool
102109
public function post(string $path, array $data = [])
103110
{
104111
/** @var array<string, mixed> */
105-
return $this->request($this->messageFactory->createRequest(
112+
return $this->request($this->createRequest(
106113
'POST',
107114
$this->createUrl($path),
108115
$this->getDefaultHeaders(),
@@ -116,7 +123,7 @@ public function post(string $path, array $data = [])
116123
*/
117124
public function put(string $path, array $data = []): array
118125
{
119-
$ret = $this->request($this->messageFactory->createRequest(
126+
$ret = $this->request($this->createRequest(
120127
'PUT',
121128
$this->createUrl($path),
122129
$this->getDefaultHeaders(),
@@ -136,7 +143,7 @@ public function put(string $path, array $data = []): array
136143
*/
137144
public function patch(string $path, array $data = [])
138145
{
139-
$ret = $this->request($this->messageFactory->createRequest(
146+
$ret = $this->request($this->createRequest(
140147
'PATCH',
141148
$this->createUrl($path),
142149
$this->getDefaultHeaders(),
@@ -298,11 +305,30 @@ protected function formatError($data): string
298305
/**
299306
* @param array<string,string> $queryParams
300307
*/
301-
protected function createUrl(string $path, array $queryParams = []): string
308+
protected function createUrl(string $path, array $queryParams = []): UriInterface
302309
{
303-
$ret = $this->apiEndpoint . '/' . $path;
310+
$url = $this->apiEndpoint . '/' . $path;
304311
if ($queryParams) {
305-
$ret .= '?' . http_build_query($queryParams);
312+
$url .= '?' . http_build_query($queryParams);
313+
}
314+
return $this->uriFactory->createUri($url);
315+
}
316+
317+
/**
318+
* @param array<string, string|string[]> $headers
319+
*/
320+
protected function createRequest(
321+
string $method,
322+
UriInterface $uri,
323+
array $headers = [],
324+
?string $body = null
325+
): RequestInterface {
326+
$ret = $this->messageFactory->createRequest($method, $uri);
327+
foreach ($headers as $key => $value) {
328+
$ret = $ret->withHeader($key, $value);
329+
}
330+
if ($body) {
331+
$ret = $ret->withBody($this->streamFactory->createStream($body));
306332
}
307333
return $ret;
308334
}

tests/AbstractTestCase.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
use Http\Client\Curl\Client as HttpClient;
66
use Monolog\Handler\StreamHandler;
77
use Monolog\Logger;
8+
use Nyholm\Psr7\Factory\Psr17Factory;
89
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Client\ClientInterface;
911
use Tests\Stefna\Mailchimp\Mocks\Client;
1012

1113
abstract class AbstractTestCase extends TestCase
1214
{
1315
private const ENV_API_KEY = 'MAILCHIMP_API_KEY';
1416
private const ENV_LOG = 'MAILCHIMP_LOG';
15-
private static HttpClient $httpClient;
17+
private static ClientInterface $httpClient;
1618
private static string $apiKey;
1719

1820
public static function setUpBeforeClass(): void
@@ -25,14 +27,28 @@ public static function setUpBeforeClass(): void
2527

2628
protected function getClient(): Client
2729
{
28-
$client = new Client(self::$httpClient, self::$apiKey);
30+
$factory = new Psr17Factory();
31+
$client = new Client(
32+
self::$httpClient,
33+
self::$apiKey,
34+
$factory,
35+
$factory,
36+
$factory,
37+
);
2938
$client->setLogger($this->getLogger());
3039
return $client;
3140
}
3241

3342
protected function getRealClient(): \Stefna\Mailchimp\Client
3443
{
35-
$client = new \Stefna\Mailchimp\Client(self::$httpClient, self::$apiKey);
44+
$factory = new Psr17Factory();
45+
$client = new \Stefna\Mailchimp\Client(
46+
self::$httpClient,
47+
self::$apiKey,
48+
$factory,
49+
$factory,
50+
$factory,
51+
);
3652
$client->setLogger($this->getLogger());
3753
return $client;
3854
}

0 commit comments

Comments
 (0)