Skip to content

Commit

Permalink
Remove static Guzzle factory
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui committed Nov 14, 2024
1 parent ff2494c commit 2ea1bc8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ The library methods will return either raw PHP arrays, objects in the **\Constan

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

## Using a GuzzleHttp factory
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.

If the factory callable is set, it will be called with the appropriate $config array passed as the first parameter.

Callback function signature:
- function(array $config) : \GuzzleHttp\Client

See [graham-campbell/guzzle-factory](https://packagist.org/packages/graham-campbell/guzzle-factory) for an example factory.

## Versioning
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.

Expand Down
19 changes: 9 additions & 10 deletions Tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ class FactoryTest extends \PHPUnit\Framework\TestCase
{
public function testSetFactory() : void
{
$this->assertNull(\PHPFUI\ConstantContact\Client::getGuzzleFactory());
$callable = [GrahamCampbell\GuzzleFactory\GuzzleFactory::class, 'make'];
\PHPFUI\ConstantContact\Client::setGuzzleFactory($callable);
$this->assertEquals($callable, \PHPFUI\ConstantContact\Client::getGuzzleFactory());
\PHPFUI\ConstantContact\Client::setGuzzleFactory(null);
$client = new \PHPFUI\ConstantContact\Client('clientAPIKey', 'clientSecret', 'redirectURI');
$callable = [GrahamCampbell\GuzzleFactory\GuzzleFactory::class, 'make'];
$client->setGuzzleFactory($callable);
$this->assertEquals($callable, $client->getGuzzleFactory());
$client->setGuzzleFactory(null);
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
$config = $guzzle->getConfig('body');
Expand All @@ -28,15 +27,15 @@ public function testSetFactory() : void
$this->assertArrayHasKey('header1', $config);
$this->assertContains('HEADER1', $config);
$this->assertArrayHasKey('Cache-Control', $config);
\PHPFUI\ConstantContact\Client::setGuzzleFactory($callable);
$guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']);
$client->setGuzzleFactory($callable);
$guzzle = $client->getGuzzleClient('body2', ['header2' => 'HEADER2']);
$this->assertTrue($guzzle instanceof \GuzzleHttp\Client);
$config = $guzzle->getConfig('body');
$this->assertEquals('body', $config);
$this->assertEquals('body2', $config);
$config = $guzzle->getConfig('headers');
$this->assertIsArray($config);
$this->assertArrayHasKey('header1', $config);
$this->assertContains('HEADER1', $config);
$this->assertArrayHasKey('header2', $config);
$this->assertContains('HEADER2', $config);
$this->assertArrayHasKey('Cache-Control', $config);
}
}
30 changes: 24 additions & 6 deletions src/ConstantContact/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Client

private string $body = '';

private static $guzzleFactory = null;
private $guzzleFactory = null;

private \GuzzleHttp\HandlerStack $guzzleHandler;

Expand Down Expand Up @@ -213,6 +213,13 @@ public function getBody() : string
return $this->body;
}

/**
* Return a new GuzzleHttp\Client with the appropriate headers and handlers set.
*
* If a Guzzle factory has been set, then the factory method will be called.
*
* @param array<string, string> $headers override the default headers
*/
public function getGuzzleClient(string $body = '', array $headers = []) : \GuzzleHttp\Client
{
$config = [
Expand All @@ -224,12 +231,12 @@ public function getGuzzleClient(string $body = '', array $headers = []) : \Guzzl
$config['body'] = $body;
}

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

public static function getGuzzleFactory() : ?callable
public function getGuzzleFactory() : ?callable
{
return self::$guzzleFactory;
return $this->guzzleFactory;
}

public function getLastError() : string
Expand Down Expand Up @@ -359,9 +366,20 @@ public function removeScope(string $scope) : self
return $this;
}

public static function setGuzzleFactory(?callable $factory) : void
/**
* 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.
*
* If the factory callable is set, it will be called with the appropriate $config array passed as the first parameter.
*
* Callback function signature:
*
* - function(array $config) : \GuzzleHttp\Client
*
* @see [graham-campbell/guzzle-factory](https://packagist.org/packages/graham-campbell/guzzle-factory) for an example factory
*/
public function setGuzzleFactory(?callable $factory) : void
{
self::$guzzleFactory = $factory;
$this->guzzleFactory = $factory;
}

public function setHost(string $host) : self
Expand Down

0 comments on commit 2ea1bc8

Please sign in to comment.