Skip to content

Commit ff2494c

Browse files
committed
Allow for a GuzzleFactory
1 parent 5bff968 commit ff2494c

File tree

4 files changed

+84
-18
lines changed

4 files changed

+84
-18
lines changed

Tests/FactoryTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPFUI\ConstantContact package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source
10+
* code
11+
*/
12+
class FactoryTest extends \PHPUnit\Framework\TestCase
13+
{
14+
public function testSetFactory() : void
15+
{
16+
$this->assertNull(\PHPFUI\ConstantContact\Client::getGuzzleFactory());
17+
$callable = [GrahamCampbell\GuzzleFactory\GuzzleFactory::class, 'make'];
18+
\PHPFUI\ConstantContact\Client::setGuzzleFactory($callable);
19+
$this->assertEquals($callable, \PHPFUI\ConstantContact\Client::getGuzzleFactory());
20+
\PHPFUI\ConstantContact\Client::setGuzzleFactory(null);
21+
$client = new \PHPFUI\ConstantContact\Client('clientAPIKey', 'clientSecret', 'redirectURI');
22+
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
23+
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
24+
$config = $guzzle->getConfig('body');
25+
$this->assertEquals('body', $config);
26+
$config = $guzzle->getConfig('headers');
27+
$this->assertIsArray($config);
28+
$this->assertArrayHasKey('header1', $config);
29+
$this->assertContains('HEADER1', $config);
30+
$this->assertArrayHasKey('Cache-Control', $config);
31+
\PHPFUI\ConstantContact\Client::setGuzzleFactory($callable);
32+
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
33+
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
34+
$config = $guzzle->getConfig('body');
35+
$this->assertEquals('body', $config);
36+
$config = $guzzle->getConfig('headers');
37+
$this->assertIsArray($config);
38+
$this->assertArrayHasKey('header1', $config);
39+
$this->assertContains('HEADER1', $config);
40+
$this->assertArrayHasKey('Cache-Control', $config);
41+
}
42+
}

Tests/HydrateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function testHydration() : void
6767
$this->assertEquals('[email protected]', $contacts->contacts[0]->email_address->address);
6868
$this->assertEquals('[email protected]', $contacts->contacts[1]->email_address->address);
6969
$time = new \PHPFUI\ConstantContact\DateTime("2024-11-08T10:54:32Z");
70-
$this->assertEquals("{$time}", "{$contacts->contacts[0]->created_at}");
71-
$this->assertEquals("{$time}", "{$contacts->contacts[1]->created_at}");
70+
$this->assertEquals((string)$time, (string)$contacts->contacts[0]->created_at);
71+
$this->assertEquals((string)$time, (string)$contacts->contacts[1]->created_at);
7272
$newJson = $contacts->getJSON();
7373
$this->assertJson($newJson);
7474
$newDataArray = $contacts->toArray();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"roave/security-advisories": "dev-latest",
2424
"friendsofphp/php-cs-fixer": "^3.0",
2525
"symfony/yaml": "^7.0",
26-
"phpstan/phpstan": "^2.0"
26+
"phpstan/phpstan": "^2.0",
27+
"graham-campbell/guzzle-factory": "^7.0"
2728
},
2829
"autoload": {
2930
"psr-4": {"PHPFUI\\ConstantContact\\": "src/ConstantContact/"}

src/ConstantContact/Client.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Client
2020

2121
private string $body = '';
2222

23+
private static $guzzleFactory = null;
24+
2325
private \GuzzleHttp\HandlerStack $guzzleHandler;
2426

2527
private string $host = '';
@@ -124,8 +126,7 @@ public function delete(string $url) : ?array
124126
{
125127
try
126128
{
127-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
128-
$response = $guzzle->request('DELETE', $url);
129+
$response = $this->getGuzzleClient()->request('DELETE', $url);
129130

130131
return $this->process($response);
131132
}
@@ -156,8 +157,7 @@ public function get(string $url, array $parameters) : ?array
156157
}
157158
}
158159

159-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
160-
$response = $guzzle->request('GET', $url);
160+
$response = $this->getGuzzleClient()->request('GET', $url);
161161

162162
return $this->process($response);
163163
}
@@ -213,11 +213,35 @@ public function getBody() : string
213213
return $this->body;
214214
}
215215

216+
public function getGuzzleClient(string $body = '', array $headers = []) : \GuzzleHttp\Client
217+
{
218+
$config = [
219+
'headers' => $this->getHeaders($headers),
220+
'handler' => $this->guzzleHandler, ];
221+
222+
if (\strlen($body))
223+
{
224+
$config['body'] = $body;
225+
}
226+
227+
return self::$guzzleFactory ? \call_user_func(self::$guzzleFactory, $config) : new \GuzzleHttp\Client($config);
228+
}
229+
230+
public static function getGuzzleFactory() : ?callable
231+
{
232+
return self::$guzzleFactory;
233+
}
234+
216235
public function getLastError() : string
217236
{
218237
return $this->lastError;
219238
}
220239

240+
public function getSessionCallback() : ?callable
241+
{
242+
return $this->sessionCallback;
243+
}
244+
221245
public function getStatusCode() : int
222246
{
223247
return $this->statusCode;
@@ -235,8 +259,7 @@ public function next() : array
235259
return [];
236260
}
237261

238-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]);
239-
$response = $guzzle->request('GET', 'https://api.cc.email' . $this->next);
262+
$response = $this->getGuzzleClient()->request('GET', 'https://api.cc.email' . $this->next);
240263

241264
return $this->process($response);
242265
}
@@ -257,10 +280,7 @@ public function post(string $url, array $parameters) : ?array
257280
try
258281
{
259282
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
260-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(),
261-
'handler' => $this->guzzleHandler,
262-
'body' => $json, ]);
263-
$response = $guzzle->request('POST', $url);
283+
$response = $this->getGuzzleClient()->request('POST', $url);
264284

265285
return $this->process($response);
266286
}
@@ -281,18 +301,16 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ?a
281301
try
282302
{
283303
$json = \json_encode($parameters['body'], JSON_PRETTY_PRINT);
284-
$guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(
304+
$guzzle = $this->getGuzzleClient(
305+
$json,
285306
[
286307
'Connection' => 'keep-alive',
287308
'Content-Length' => \strlen($json),
288309
'Accept-Encoding' => 'gzip, deflate',
289310
'Host' => $this->host,
290311
'Accept' => '*/*'
291312
]
292-
),
293-
'handler' => $this->guzzleHandler,
294-
'body' => $json, ]);
295-
313+
);
296314
$response = $guzzle->request($method, $url);
297315

298316
return $this->process($response);
@@ -341,6 +359,11 @@ public function removeScope(string $scope) : self
341359
return $this;
342360
}
343361

362+
public static function setGuzzleFactory(?callable $factory) : void
363+
{
364+
self::$guzzleFactory = $factory;
365+
}
366+
344367
public function setHost(string $host) : self
345368
{
346369
$this->host = $host;

0 commit comments

Comments
 (0)