Skip to content

Commit 2ea1bc8

Browse files
committed
Remove static Guzzle factory
1 parent ff2494c commit 2ea1bc8

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ The library methods will return either raw PHP arrays, objects in the **\Constan
100100

101101
If a raw or typed method returns null, then an error occured and you should check $client->getStatusCode() or $client->getLastError() for more information.
102102

103+
## Using a GuzzleHttp factory
104+
This library uses **\GuzzleHttp\Client** to make CC API calls. If you want to use your own **\GuzzleHttp\Client**, you should create a factory callable method and set it.
105+
106+
If the factory callable is set, it will be called with the appropriate $config array passed as the first parameter.
107+
108+
Callback function signature:
109+
- function(array $config) : \GuzzleHttp\Client
110+
111+
See [graham-campbell/guzzle-factory](https://packagist.org/packages/graham-campbell/guzzle-factory) for an example factory.
112+
103113
## Versioning
104114
Since the [Constant Contact API](https://v3.developer.constantcontact.com/api_guide/index.html) is constantly being updated, this library will track all updates on a calendar based versioning schema. The major version will be the last two digits of the year the update was released. The minor version will be the month it was released. Any bug fixes will be a patch version. So V21.8.0 would be the first August 2021 version, and V21.8.1 would be a bug fix to V21.8. All bug fixes will be included in subsequent versions, so V21.9.0 would include all fixes from the V21.8 version. YAML changes are tracked nightly and a new version will be generated automatically. Multiple YAML changes in a month will be tracked as patch versions.
105115

Tests/FactoryTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
1313
{
1414
public function testSetFactory() : void
1515
{
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);
2116
$client = new \PHPFUI\ConstantContact\Client('clientAPIKey', 'clientSecret', 'redirectURI');
17+
$callable = [GrahamCampbell\GuzzleFactory\GuzzleFactory::class, 'make'];
18+
$client->setGuzzleFactory($callable);
19+
$this->assertEquals($callable, $client->getGuzzleFactory());
20+
$client->setGuzzleFactory(null);
2221
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
2322
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
2423
$config = $guzzle->getConfig('body');
@@ -28,15 +27,15 @@ public function testSetFactory() : void
2827
$this->assertArrayHasKey('header1', $config);
2928
$this->assertContains('HEADER1', $config);
3029
$this->assertArrayHasKey('Cache-Control', $config);
31-
\PHPFUI\ConstantContact\Client::setGuzzleFactory($callable);
32-
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
30+
$client->setGuzzleFactory($callable);
31+
$guzzle = $client->getGuzzleClient('body2', ['header2' => 'HEADER2']);
3332
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
3433
$config = $guzzle->getConfig('body');
35-
$this->assertEquals('body', $config);
34+
$this->assertEquals('body2', $config);
3635
$config = $guzzle->getConfig('headers');
3736
$this->assertIsArray($config);
38-
$this->assertArrayHasKey('header1', $config);
39-
$this->assertContains('HEADER1', $config);
37+
$this->assertArrayHasKey('header2', $config);
38+
$this->assertContains('HEADER2', $config);
4039
$this->assertArrayHasKey('Cache-Control', $config);
4140
}
4241
}

src/ConstantContact/Client.php

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

2121
private string $body = '';
2222

23-
private static $guzzleFactory = null;
23+
private $guzzleFactory = null;
2424

2525
private \GuzzleHttp\HandlerStack $guzzleHandler;
2626

@@ -213,6 +213,13 @@ public function getBody() : string
213213
return $this->body;
214214
}
215215

216+
/**
217+
* Return a new GuzzleHttp\Client with the appropriate headers and handlers set.
218+
*
219+
* If a Guzzle factory has been set, then the factory method will be called.
220+
*
221+
* @param array<string, string> $headers override the default headers
222+
*/
216223
public function getGuzzleClient(string $body = '', array $headers = []) : \GuzzleHttp\Client
217224
{
218225
$config = [
@@ -224,12 +231,12 @@ public function getGuzzleClient(string $body = '', array $headers = []) : \Guzzl
224231
$config['body'] = $body;
225232
}
226233

227-
return self::$guzzleFactory ? \call_user_func(self::$guzzleFactory, $config) : new \GuzzleHttp\Client($config);
234+
return $this->guzzleFactory ? \call_user_func($this->guzzleFactory, $config) : new \GuzzleHttp\Client($config);
228235
}
229236

230-
public static function getGuzzleFactory() : ?callable
237+
public function getGuzzleFactory() : ?callable
231238
{
232-
return self::$guzzleFactory;
239+
return $this->guzzleFactory;
233240
}
234241

235242
public function getLastError() : string
@@ -359,9 +366,20 @@ public function removeScope(string $scope) : self
359366
return $this;
360367
}
361368

362-
public static function setGuzzleFactory(?callable $factory) : void
369+
/**
370+
* This library uses GuzzleHttp\Client to make CC API calls. If you want to use your own GuzzleHttp\Client, you should create a factory callable method and set it.
371+
*
372+
* If the factory callable is set, it will be called with the appropriate $config array passed as the first parameter.
373+
*
374+
* Callback function signature:
375+
*
376+
* - function(array $config) : \GuzzleHttp\Client
377+
*
378+
* @see [graham-campbell/guzzle-factory](https://packagist.org/packages/graham-campbell/guzzle-factory) for an example factory
379+
*/
380+
public function setGuzzleFactory(?callable $factory) : void
363381
{
364-
self::$guzzleFactory = $factory;
382+
$this->guzzleFactory = $factory;
365383
}
366384

367385
public function setHost(string $host) : self

0 commit comments

Comments
 (0)